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

Thread: Display text file content into the console in sequence order ?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Display text file content into the console in sequence order ?

    Hello,
    I am trying to display the contents in my text file into the console in sequence order, so it looks like an action replay from user inputs.
    this is my text file:

    Kardo
    1
    2
    Kalio
    1
    1
    Kardo
    2
    2
    Kalio
    2
    1
    Kardo
    3
    2

    this is what i have so far:
    System.out.print("Woud you like to replay the actions (y/n)? ");
    String answer = input.getPrompt();
    if (answer.equalsIgnoreCase("Y")) {
    File file = new File("Actions.txt");

    try {

    Scanner scanner = new Scanner(file);

    while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    System.out.println(line);

    }
    scanner.close();

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    System.exit(0); // terminate the program

    }

    any help on how to do it would be great!
    (I am a beginner)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display text file content into the console in sequence order ?

    What is the program's output supposed to look like? What does the program output now?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display text file content into the console in sequence order ?

    the output should be the text file content in the java console but in a sequence order (ie: 3 seconds delay between each display),
    at the moment it just gives me a blank display.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display text file content into the console in sequence order ?

    Please post what the program's output should be when it reads the contents of the file shown in post#1. I have no idea what you mean by "in a sequence order".

    Also post the program's current output.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Display text file content into the console in sequence order ?

    the current output is blank, by sequence order i mean to display the contents in the text file in order, this is my text file's content from user input:
    Kardo
    1
    2
    Kalio
    1
    1
    Kardo
    2
    2
    Kalio
    2
    1
    Kardo
    3
    2



    i want to display it in the console one by one :
    for example:
    kardo
    (2 seconds)
    1
    (2 seconds)
    2
    (2 seconds)
    kalio
    (2 seconds)
    .
    .
    .

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display text file content into the console in sequence order ?

    If you are asking how to have the program pause for 2 seconds between printing to the console, look at the Thread class's sleep() method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display text file content into the console in sequence order ?

    no i am programming a simple tik tak toe game, my aim is to record all the player inputs and at the end do action replay and display all the moves in the console.
    i have already designed the game, but i am unable to replay the actions into the console.
    any help would be great

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display text file content into the console in sequence order ?

    Sorry, you lost me. I thought you wanted to have a pause between the display of lines on the console.

    What is the problem you are having doing: " replay the actions into the console."
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display text file content into the console in sequence order ?

    that is correct, i am trying to replay the actions into the console but with a little pause. my code at the moment does not display any thing, i would be greatful if you could give me an assistance with my code.
    what i have :
    System.out.print("Woud you like to replay the actions (y/n)? ");
    String answer = input.getPrompt();
    if (answer.equalsIgnoreCase("Y")) {	
    File file = new File("Actions.txt");
     
    try {
     
    Scanner scanner = new Scanner(file);
     
    while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    System.out.println(line);
     
    }
    scanner.close();
     
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    System.exit(0); // terminate the program
     
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display text file content into the console in sequence order ?

    The posted code can't be compiled and executed for testing to see what it does. You need to post a small, complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM
  2. Content in text file to MS Word file help!
    By ComputerSaysNo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 27th, 2011, 11:39 AM
  3. reading content of the text file, splitting it into strings
    By Dodo in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 6th, 2011, 07:57 PM
  4. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM
  5. Buffered Reader to display file content after calcuation
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 19
    Last Post: May 14th, 2008, 03:23 AM