Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Writing Integers to a file

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Writing Integers to a file

    I'm trying to ask the user to enter 7 scores by writetofile method by a loop that get written on a file on a separate line.

    This is what I have so far:
    public static void main(String[] args) throws IOException {
            writeToFile ("c:\\scores.txt");
    }
     
    public static void writeToFile (String filename) throws IOException {
            BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
            Scanner reader = new Scanner (System.in);
            int Num;
            System.out.println ("Please enter 7 scores");
            Num= reader.nextInt ();
            while !(Num < 0) { 
            for (int i = 0; i <= 7; i++)
               Num= reader.nextInt ();
               outputWriter.write(Num);
               outputWriter.newLine();
            }
            outputWriter.flush(); 
            outputWriter.close();
    }
    On this line, while !(Num < 0) { the error is '(' expected...how do I fix this?
    Also, the when I run the program, it asks for 7 scores, but it just keeps going with the user typing integers. Also, the file gets cleared and no scores show up there.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing Integers to a file

    The condition for the while statement should be completely inside the ()s.

    How can the code execute with compiler errors?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing Integers to a file

    I fixed it, but the problem now is that it still takes more than 7 scores...and it does not seem to just stop.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing Integers to a file

    There are a couple of coding conventions you are not following:
    You should always use {}s with loops to enclose the code that is in the loop. Same for if statements.
    Nested statements within loops should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing Integers to a file

    Is this better?
     public static void writeToFile (String filename) throws IOException {
            BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
            Scanner reader = new Scanner (System.in);
            int Num;
            System.out.println ("Please enter 7 scores");
            Num= reader.nextInt ();
            while (!(Num < 0)) { 
                for (int i = 0; i <= 7; i++) {
                    Num= reader.nextInt ();
                    outputWriter.write(Num);
                    outputWriter.newLine();
                }
            }
            outputWriter.flush(); 
            outputWriter.close();
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing Integers to a file

    How does the code execute now?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing Integers to a file

    It still has the same problems as before.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing Integers to a file

    How many numbers does it read?
    How many numbers are written to the file?
    What happens to the first number that is read?

    When do you expect the code to exit the while() loop?
    What is the value of Num after the code exits the for loop? Add a println statement to print its value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing Integers to a file

    This is what happens
    Please enter 7 scores
    1
    2
    3
    4
    5
    6
    7
    5
    Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1011)
    at java.lang.Double.parseDouble(Double.java:540)
    at writefile.Writefile.processFile(Writefile.java:46)
    at writefile.Writefile.main(Writefile.java:19)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 4 minutes 25 seconds)
    It should read 7. How can i check the file? I entered 7 numbers at first, but nothing happened,but once I type in the eighth integer it gives me an error.
    This is my code by the way.
    public static void writeToFile (String filename) throws IOException {
            BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
            Scanner reader = new Scanner (System.in);
            int Num;
            System.out.println ("Please enter 7 scores");
                for (int i = 0; i <= 7; i++) {
                    Num= reader.nextInt ();
                    outputWriter.write(Num);
                    outputWriter.newLine();
                }
            outputWriter.flush(); 
            outputWriter.close();
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing Integers to a file

    Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1011)
    at java.lang.Double.parseDouble(Double.java:540)
    at writefile.Writefile.processFile(Writefile.java:46)
    at writefile.Writefile.main(Writefile.java:19)
    That error happens in the processFile() method at line 46 where it calls the parseDouble() method.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing Integers to a file

    How do I fix this error?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing Integers to a file

    Test the variable being passes to the parseDouble() method and make sure it is not an empty string:
    If string is not empty
    call parseDouble
    else
    don't call parseDouble
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing Integers to a file

    I don't know how to do that because I thought one of the number I typed in would get printed onto the file.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing Integers to a file

    Use an if statement to test if the String is empty and don't use it if it is.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Adding integers from a file
    By LoganC in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 1st, 2012, 04:10 PM
  2. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM