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

Thread: Reading integers from a file, but it's only displaying the first number repeatedly..?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reading integers from a file, but it's only displaying the first number repeatedly..?

    Hi! I am new here and currently taking my first CS class, which happens to be Java. Usually I do very well with the assignments and have no trouble, but right now I am completely stumped.

    We just learned file input/output and loops, so our assignment is to read 30 scores (integers) from a file and display them using regular output. The only problem is, I'm supposed to use a for loop to only read the first 30 numbers.

    So far, I can only get my code to display the first number of the file repeatedly. I am completely stumped!

    This is what my code looks like:

    import java.util.Scanner;
    import java.io.*;
     
    public class Echo
     
    {
    public static void main(String[] args) throws IOException
     
    {
     
    int number;
    int score;
    File file = new File("scores.txt");
    Scanner inputFile = new Scanner(file);
     
     
     
     
    score = inputFile.nextInt();
     
    for(number=1; number<=30; number=number+1)
    {System.out.println(score);}
     
     
     
    inputFile.close();
     
     
     
    }
     
    }


    I'm sure it has to be something simple that I'm overlooking. Any help is appreciated! Thanks.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Reading integers from a file, but it's only displaying the first number repeatedl

    Well you're only reading from the file once, where you assign the first int to score, then all you do is print out that score variable 30 times.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading integers from a file, but it's only displaying the first number repeatedl

    Honestly, I'm just very confused right now. The program is printing the first number of the file 30 times. How can I get it to take the first 30 numbers and print them without assigning them each a variable?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Reading integers from a file, but it's only displaying the first number repeatedl

    Look up the Scanner API, specifically the hasNextInt() method.
    Scanner (Java 2 Platform SE 5.0)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Reading integers from a file, but it's only displaying the first number repeatedl

    As it was said, All your doing is printing the number alot .. Try to rescan in your for loop

    for(int number=1;number<=30;number++)
    {
    score = inputFile.nextInt();
    System.out.println(score);
    }

  6. The Following User Says Thank You to Tjstretch For This Useful Post:

    jessica202 (April 13th, 2011)

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Reading integers from a file, but it's only displaying the first number repeatedl

    Scanner scanner = new Scanner(new File("abc.txt"));
    int [] show = new int [100];
    int i = 0;
    while(scanner.hasNextInt()){
        show[i++] = scanner.nextInt();
     }
    Remember to catch necessary exceptions.

Similar Threads

  1. [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
  2. 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
  3. repeatedly reading from a url? anyone?
    By tandren544 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 15th, 2010, 04:34 PM
  4. [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
  5. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM