• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

reading a file into my program.

I am trying to tell the program to always read in the file named, Competitors.txt but all I get is an error.  i don't know what I am doing wrong here is my code.

import java.lang.*;
import java.util.*;
import java.io.*;

public class ContestManagement {
      
      public static void main (String [] args)  throws IOException{

      //define local variables
      final int MAX_LENGTH = 30;
      Competitor competitorArray[] = new Competitor[MAX_LENGTH];
      int arraySize = 0;

      Scanner stdin = new Scanner(System.in);
 
//set up file stream
      Scanner fileIn = null;
      
      try {
            fileIn = new Scanner("Competitors.txt");
            for (int i = 0; (fileIn.hasNextLine()) && (i < MAX_LENGTH); ++i) {
                  int ID = fileIn.nextInt();
                  String n = fileIn.next();
                  double m = 0.0;
                  Competitor c = new Competitor(ID, n, m);
                  competitorArray[i] = c;
                  ++arraySize;
            }
      }
      catch (InputMismatchException e) {
            System.out.println("uh oh, got an exception error!");
      }
      
    }
}            
0
rebelcowgirl_73
Asked:
rebelcowgirl_73
5 Solutions
 
ravs120499Commented:
A source code listing and a mention that it throws an error is usually not enough to debug.

At what line do you get the error? What is the stack trace? What is the Scanner class - do you have its source code?

- Ravs
0
 
netsmithcentralCommented:
The Scanner class is from the standard Java API (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html).  You're using the wrong Scanner constructor to access a file though.  The constructor you're using is to scan the actual string you pass the constructor.  Here's what you want instead.

fileIn = new Scanner(new File("Competitors.txt"));
0
 
rebelcowgirl_73Author Commented:
no debug error just when I run the program the catch statement prints.  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
check the file exists
that it is in the correct format
and it doesn't have any blank lines
and that it has less than 30 lines
0
 
rebelcowgirl_73Author Commented:
Competitors.txt contains the following with no black lines appears just like this!!!!

6241 Cathy
3792 Patrick
2910 John
8371 Mary
9372 Tony
3532 Jane
0
 
rebelcowgirl_73Author Commented:
Ok I think I got it, however, I cant seem to print the array now without it print a line for each null value in the array.  Meaning the print has 5 lines of data and then 25 null values because of the MAX_VALUE = 30

Here is what I have!

public class ContestManagement {
      
      public static void main (String [] args)  throws IOException{

      //define local variables
      final int MAX_LENGTH = 30;
      Competitor competitorArray[] = new Competitor[MAX_LENGTH];
      int arraySize = 0;

      Scanner stdin = new Scanner(System.in);

      //set up file stream
        Scanner fileIn = null;
        StringTokenizer st = null;
        try {
            BufferedReader in = new BufferedReader(new FileReader("Competitors.txt"));
            String s = null;
            while((s = in.readLine()) != null) {
                  System.out.println(s);
                  st = new StringTokenizer(s);
                  int ID = Integer.parseInt(st.nextToken());
                  String n = st.nextToken();
                  double m = 0.0;
                  Competitor c = new Competitor(ID, n, m);
                  competitorArray[arraySize] = c;
                  ++arraySize;
            }
            for (int i = 0; i < MAX_LENGTH; ++i) {
                  System.out.println(competitorArray[i]);
            }
      }
      catch (InputMismatchException e) {
            System.out.println("uh oh, got an exception error!");
      }
    }
}      

the system.out.println(s); in the while statement is just for my own personal verification that the file was being read.

the for statement is where i am trying to print the array, but it is printing all 30 lines and not just the ones that have information in it.      
0
 
rebelcowgirl_73Author Commented:
Also the Competitor class implements comparable and the use of the toString() which is how I am printing if that helps!
0
 
CPlusJavaCSharpCommented:
What about

Scanner fileIn = new Scanner(new File("Competitors.txt"));
while(fileIn.hasNext())
{
   int ID = fileIn.nextInt();
   String in = fileIn.next();
  ...

}
0
 
CPlusJavaCSharpCommented:
Why use a bufferreader when you can use a Scanner/File- which you never use...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now