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 13 of 13

Thread: File input and file output

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default File input and file output

    Could someone help me figure out why my program won't input the data back onto the screen after it output it onto a text file
    Thanks


    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.File;
    import java.util.Scanner;
    import java.util.Random;
     
    public class BottleCapPrize
    {
        public static void main(String[]args) throws IOException
        {
            PrintWriter outFile = new PrintWriter(new File("MonteCarlo.txt"));
            Scanner inFile = new Scanner(new File("MonteCarlo.txt"));
            Random randNum = new Random();
            int bottleCapNum = 0;
            int wins = 0;
            int bottleCapInput = 0;
            int token;
     
            Scanner userInput = new Scanner (System.in);
     
            //
            System.out.print("Please enter the number of trials: ");
            int uInput = userInput.nextInt();
     
           for (int counter = 1; counter <= uInput; counter++) {  
              int randNum1 = 0;  
              while (randNum1 != 1) {  
                        randNum1 = randNum.nextInt(5);  
                        System.out.println("randNum1 = " + randNum1);  
                        bottleCapNum++;  
                    }  
              wins++;  
              bottleCapNum++;  
              System.out.println("Counter = " + counter + " bottleCapNum = "  
                            + bottleCapNum + " wins = " + wins);
              outFile.println(bottleCapNum);
     
                }  
           outFile.close( );
            while(inFile.hasNextInt()); 
              { token = inFile.nextInt();
             System.out.println(token);}
               inFile.close();
     
            }
            }


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

    Default Re: File input and file output

    What does the code print out when it executes?

    Does the program end normally?

    Add some println statements so you can see where the code is executing and find the last statement that it executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: File input and file output

    I'm trying to make the program output the number into the file and then get it back from that file.

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

    Default Re: File input and file output

    What does the code print out when it executes?

    Does the program end normally?

    Add some println statements so you can see where the code is executing and find the last statement that it executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: File input and file output

    It would output the result to the text file but it seem the program is not ending. It get stuck I'm guessing at the line to read the text file and print them on the screen.

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

    Default Re: File input and file output

    Try adding some println statements to see where the code is hanging/stuck. Sometime you need them every other statement so you can tell which statement the program is hanging on. If one println doesn't print, then you know that the program is hung on the statement just in front of it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: File input and file output

    The code is stoping after it print "outFile.println(bottleCapNum);" So the while loop to read the file is where the program stuck at.

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

    Default Re: File input and file output

    What statement is the one before the last println statement that did not print?

    EDIT: corrected above

    When it did not print, that means the statement before it is hung.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: File input and file output

    while(inFile.hasNextInt());
    { token = inFile.nextInt();

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

    Default Re: File input and file output

    That's two statements. Which one is it hanging on? Put a println between them.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: File input and file output

    This is what I put

    while(inFile.hasNextInt());
    {System.out.println(inFile);
    token = inFile.nextInt();
    System.out.println(token);}

    But it did not print the inFile so it hanging at the start of the while loop.

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

    Default Re: File input and file output

    But it did not print the inFile so it hanging at the start of the while loop.
    Take a close look at the while statement and make sure there isn't a statement ending ; before the { that surrounds the code that is supposed to be in the loop.

    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following 2 Users Say Thank You to Norm For This Useful Post:

    jtmnf (December 27th, 2012), maple1100 (December 26th, 2012)

  14. #13
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: File input and file output

    Oh wow... I miss such a little detail. Thank you so much. Can't believe I been trying to figure out what wrong and it end up being that ; T~T

Similar Threads

  1. File input & output
    By a21j92 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 8th, 2013, 04:13 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. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 07:44 AM
  4. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 08:34 PM
  5. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 06:26 PM