13 Replies Latest reply on Mar 12, 2008 7:29 AM by 807591

    Appending to an existing file

    807591
      Am i correctly appending to the file with this addToData method. When i input temps to be added, still no file is created...
         public static void addToDataFile() throws IOException
          {
              double userTemp=0.0;
              PrintWriter outFile;
              Scanner keyboard=new Scanner(System.in);
              
              System.out.println("\nWhat other temperatures would you like to see in the report?"
                  +"\nEnter the temperature in centigrade, "
                  +"\none per line in the range "+MINIMUM_VALUE+" to "+MAX_VALUE+"or "+SENTINEL_VALUE
                  +" to quit.");
              while(userTemp!=SENTINEL_VALUE)
              {
                  System.out.print("\nTemperature to add ("+MINIMUM_VALUE+" to "+MAX_VALUE
                  +" or "+SENTINEL_VALUE+" to quit)");
                  userTemp=keyboard.nextDouble();
              
                  while(userTemp < MINIMUM_VALUE || userTemp > MAX_VALUE && userTemp != SENTINEL_VALUE)
                  {
                      System.out.println("ERROR - Temperature entered is out of range.");
                      System.out.print("\nTemperature to add ("+MINIMUM_VALUE+" to "+MAX_VALUE
                      +" or "+SENTINEL_VALUE+" to quit)");
                      userTemp=keyboard.nextDouble();
                  }
                  outFile=new PrintWriter(new FileWriter(TEMPERATURE_REPORT_FILE,true));
                  outFile.print(userTemp);
                  outFile.close();
              }
              
          }
        • 1. Re: Appending to an existing file
          807591
          Why don't you go back and read your previous responces. This is the SAME CODE that you asked for help on before and you've still NOT IMPLEMENTED ANY of the previous suggestions.

          YOUR CODE IS STILL WRONG!!!

          and we are still not going to completely correct your project for you
          • 2. Re: Appending to an existing file
            807591
            u mean when i asked about the while loop? I got that portion of the method to work...but now i need to write the temps inputted to the temperature data file, i read that if you pass true to the FileWriter it will append to and existing file, but it isnt writing anything to the file.
            • 3. Re: Appending to an existing file
              807591
              maryjanelane wrote:
              u mean when i asked about the while loop? I got that portion of the method to work...but now i need to write the temps inputted to the temperature data file, i read that if you pass true to the FileWriter it will append to and existing file, but it isnt writing anything to the file.
              Then post the updated code, not the same code you posted before. As far as the people that helped you before, this tread is dead, since you don't seem to implement any solutions, you just keep posting the same old code. Post the new stuff and ask again.
              • 4. Re: Appending to an existing file
                807591
                that method is different from the code in the earlier message in the following ways:

                -The PrintWriter object in the declaration.
                -Implementation in the data validation loop of &&userTemp!=SENTINEL_VALUE (where i was having problems before)
                -The FileWriter near the end of the method which is intended to write each valid value into the report file. (it doesnt, therefore i believe i am implementing some part incorrectly)
                • 5. Re: Appending to an existing file
                  807591
                  maryjanelane wrote:
                  that method is different from the code in the earlier message in the following ways:

                  -The PrintWriter object in the declaration.
                  -Implementation in the data validation loop of &&userTemp!=SENTINEL_VALUE (where i was having problems before)
                  -The FileWriter near the end of the method which is intended to write each valid value into the report file. (it doesnt, therefore i believe i am implementing some part incorrectly)
                  Then why did you post the same old code and ask a new question about it in a new thread? Like I said---post your new code or at least the relavent code you are talking about.
                  • 6. Re: Appending to an existing file
                    807591
                    I just realized i was appending to the wrong data file, so i switched them, but now im getting a new error:
                    java.lang.NumberFormatException: multiple points
                         at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1084)
                         at java.lang.Double.parseDouble(Double.java:510)
                         at TempReport.buildReport(TempReport.java:93)
                         at TempReport.main(TempReport.java:36)
                    heres the code:
                    public class TempReport
                    {
                        public static final double  FREEZING_POINT=0.0;
                        public static final double  BOILING_POINT=100.0;
                        public static final double ROOM_TEMP=22.0;
                        public static final double MINIMUM_VALUE=-500.0;
                        public static final double MAX_VALUE=500.0;
                        public static final double SENTINEL_VALUE=999.0;
                        public static final String TEMPERATURE_REPORT_FILE="TempReport.txt";
                        public static final String CENTIGRADE_DATA_FILE = "CentigradeTemps.dat";
                        
                        //************* MAIN METHOD ********************************************
                        public static void main(String[] args) throws IOException
                        {
                            System.out.println("CSC 15 - Temperature Assignment"
                                              + "      by Matt Mattson\n");
                            printFile(CENTIGRADE_DATA_FILE);
                            buildReport();
                            printFile(TEMPERATURE_REPORT_FILE);
                            addToDataFile();
                            buildReport();
                            printFile(TEMPERATURE_REPORT_FILE);
                            addToDataFile();
                            buildReport();
                            printFile(TEMPERATURE_REPORT_FILE);
                            printFile(CENTIGRADE_DATA_FILE);
                        } // end main
                    
                        public static void printFile(String textFile) throws IOException
                        {
                            boolean exists=(new File(textFile)).exists();
                            String inputString="";
                            BufferedReader inputDataFile;
                            
                            //If file exists open file, read each line, print each line
                            if(exists)
                            {
                              //Open file for reading
                              inputDataFile= new BufferedReader(new FileReader(textFile));
                              
                              System.out.println("***************FILE:"+textFile+"***************");
                              
                              //While loop to read each line of file
                              while((inputString= inputDataFile.readLine())!=null)
                              {
                                System.out.println(inputString);
                              }
                              inputDataFile.close();
                            }
                            else
                            {
                                System.out.println("***************FILE:"+textFile+"***************"
                                +"\n\n The file: "+CENTIGRADE_DATA_FILE+" does not exist!\n\n"
                                +"******************************************************");
                            }
                        }
                        
                        public static void buildReport() throws IOException
                        {
                            double centigradeTemp=0.00;
                            String inputTemp="";
                            boolean exists=(new File(CENTIGRADE_DATA_FILE)).exists();
                            BufferedReader inputDataFile;
                            PrintWriter outFile;
                               //Open file writer to write report
                            outFile=new PrintWriter(new FileWriter(TEMPERATURE_REPORT_FILE));
                            
                            outFile.println("\n\t\tTEMPERATURE REPORT \n \t\t by Matt Mattson\n");
                            
                            if(exists)
                            {
                                //Open centigrade temps to be read and converted
                                inputDataFile=  new BufferedReader(new FileReader(CENTIGRADE_DATA_FILE));   
                                outFile.println("CENTIGRADE\tFAHRENHEIT\tCOMMENTS");
                                while((inputTemp= inputDataFile.readLine())!=null)
                                {
                                    outFile.print("\n"+inputTemp);
                                    centigradeTemp=Double.parseDouble(inputTemp);   
                                    outFile.print("\t\t"+fahrenheit(centigradeTemp)); 
                                    if(centigradeTemp<=FREEZING_POINT)
                                    {
                                        outFile.print("\t\tFREEZING");
                                    }
                                    else if(centigradeTemp>=BOILING_POINT)
                                    {
                                        outFile.print("\t\tBOILING");
                                    }
                                    else if(centigradeTemp==ROOM_TEMP)
                                    {
                                        outFile.print("\t\tROOM TEMP");
                                    }
                                }
                            }   
                            else
                            {
                                outFile.println("The temperature file: "+CENTIGRADE_DATA_FILE+" does not exist!\n\n"
                                +"*****************************************************");
                            }
                            outFile.close();
                        }
                        
                        public static double fahrenheit(double centigrade)
                        {
                            double fahrenTemp=0.00;
                            fahrenTemp=(9.0/5.0)*centigrade + 32;
                            return fahrenTemp;
                        }
                        
                        public static void addToDataFile() throws IOException
                        {
                            double userTemp=0.0;
                            PrintWriter outFile;
                            Scanner keyboard=new Scanner(System.in);
                            
                            System.out.println("\nWhat other temperatures would you like to see in the report?"
                                +"\nEnter the temperature in centigrade, "
                                +"\none per line in the range "+MINIMUM_VALUE+" to "+MAX_VALUE+"or "+SENTINEL_VALUE
                                +" to quit.");
                            while(userTemp!=SENTINEL_VALUE)
                            {
                                System.out.print("\nTemperature to add ("+MINIMUM_VALUE+" to "+MAX_VALUE
                                +" or "+SENTINEL_VALUE+" to quit)");
                                userTemp=keyboard.nextDouble();
                            
                                while(userTemp < MINIMUM_VALUE || userTemp > MAX_VALUE && userTemp != SENTINEL_VALUE)
                                {
                                    System.out.println("ERROR - Temperature entered is out of range.");
                                    System.out.print("\nTemperature to add ("+MINIMUM_VALUE+" to "+MAX_VALUE
                                    +" or "+SENTINEL_VALUE+" to quit)");
                                    userTemp=keyboard.nextDouble();
                                }
                                outFile=new PrintWriter(new FileWriter(CENTIGRADE_DATA_FILE,true));
                                outFile.print(userTemp);
                                outFile.close();
                            }
                            
                        }
                      
                       
                        
                        
                    
                    } // end class TempReport
                    Can somebody clue me in on what this error is telling me

                    Edited by: maryjanelane on Mar 10, 2008 5:36 PM
                    • 7. Re: Appending to an existing file
                      796365
                      It is very simple to look up the error.

                      The error message tells you that at line 93 of this program
                      at TempReport.buildReport(TempReport.java:93)
                      a java.lang.NumberFormatException occurred.

                      This is documented here:
                      http://java.sun.com/javase/6/docs/api/java/lang/NumberFormatException.html

                      with the explanation
                      Thrown to indicate that the application has attempted to convert a string to one of the numeric types, 
                      but that the string does not have the appropriate format.
                      • 8. Re: Appending to an existing file
                        807591
                        And yet, you still have NOT posted any UPDATED code. Why do you ask the questions if you are not going to implement the answers, perhaps you need to go back and start at the tutorial again or for the first time?
                        • 9. Re: Appending to an existing file
                          807591
                          Perhaps MaryJane is BillyBob's sister.
                          • 10. Re: Appending to an existing file
                            807591
                            flounder wrote:
                            Perhaps MaryJane is BillyBob's sister.
                            Perhaps, this is the 2nd or 3rd thread it has started with the same code--all at different times and soliciting various helps, but never updating the code to start the next thread. Troll any one?
                            • 11. Re: Appending to an existing file
                              807591
                              What are you talking about man, do you want me to post the finished product or what?
                              • 12. Re: Appending to an existing file
                                807591
                                maryjanelane wrote:
                                What are you talking about man, do you want me to post the finished product or what?
                                I don't know about you, but when somebody points out to me that my code is in error, then I tend to want to change it before I ever show anyone else--but then that could just be me--lest everyone know that I'm extreamly lazy or a total moron.

                                Since you keep posting the same old flawed code and asking different questions and continuing conversations in the threads posting the same flawed code, then what conclusions can we draw.

                                I'll say this: I hope you at least copy the correct answers from the discussions for your final draft that you turn in to your instructor or your boss, but then again, maybe not; I'm sure you'll not give the board credit for doing your work for you.
                                • 13. Re: Appending to an existing file
                                  807591
                                  First off let me say I didn't come here looking to affend anyone. So if I appologize if I somehow affended you by not reposting the fixed code immediately. Secondly, I am extremely new to Java, and programming in general so I am learning a lot right now and going at my own pace. Lastly, I do give this board credit, I already showed my instructor this site, and I also refer people who are having difficulty with Java to this board because I found it to be extremely helpful. Once again, I appreciate all of the help I am getting.