reading a file into my program!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    reading a file into my program!

    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.

    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!");
          }
          
        }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by AZRebelCowgirl7 3
    but all I get is an error.
    Care to tell what the 'error' is?

    kind regards,

    Jos

    Comment

    • AZRebelCowgirl73
      New Member
      • Nov 2006
      • 47

      #3
      sorry it is not a debug error, I get the catch error to print! which tells me I am not scanning the file in to the program right.

      Comment

      • AZRebelCowgirl73
        New Member
        • Nov 2006
        • 47

        #4
        and the competitors.txt file exists within the same directory and has the following information with no blank lines:

        6241 Cathy
        3792 Patrick
        2910 John
        8371 Mary
        9372 Tony
        3532 Jane

        Comment

        • AZRebelCowgirl73
          New Member
          • Nov 2006
          • 47

          #5
          Ok I think I got it, however, Here is what I have!

          Code:
                //set up file stream
                  Scanner fileIn = null;
                   try {
          	fileIn = new Scanner(new File("Competitors.txt"));
          	while(fileIn.hasNext()) {
          		int ID = fileIn.nextInt();
          		String n = fileIn.next();
          		double m = 0.0;
          		Competitor c = new Competitor(ID, n, m);
          		competitorArray[arraySize] = c;
          		++arraySize;
          	}           
                    }
                catch (InputMismatchException e) {
                      System.out.println("uh oh, got an exception error!");
                }

          Comment

          • JoeMac3313
            New Member
            • Jul 2007
            • 16

            #6
            Are you running 1.4 or 1.5 in Java because I think Scanner isn't implemented in 1.4.

            Joe

            Comment

            Working...