Java Problem

Discussion in 'OT Technology' started by Interlude, Dec 1, 2004.

  1. Interlude

    Interlude New Member

    Joined:
    Dec 25, 2003
    Messages:
    6,346
    Likes Received:
    0
    Location:
    SoCal
    1.Input filename must be read from console.

    2.The program must check to make sure hte input file exits. If not, notify the user that the user and allow the user to input the file name again.

    3.The output file must be read from console.

    When running the program, I getting this error message:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
    at ReadText.main(ReadText.java:30)

    I cant figure out what's wrong with my tokenizer, any help is appreciated :wtc:

    Code:
        import java.io.*;
        import java.util.StringTokenizer;
     	
     	public class ReadText
        {
     	   public static void main (String[]args)throws IOException
     	  {
     		 BufferedReader inFile = new BufferedReader ( new InputStreamReader (System.in));
     		 System.out.print("Enter File Name: ");
     		 String line = inFile.readLine();
     			
     			
     		 StringTokenizer tokenizer = new StringTokenizer (line);
     		  
     		 Student [] list = new Student[10];
     			  		    		 
     		 for (int i = 0; i< 10; i++)
     		 {
     			list[i] = new Student();
     		 
     				 tokenizer = new StringTokenizer(line);
     		 
     			list[i].setFirstName(tokenizer.nextToken());
     			list[i].setLastName(tokenizer.nextToken());
     			 list[i].setTestScore(1,Double.parseDouble(tokenizer.nextToken()));
     		    list[i].setTestScore(2,Double.parseDouble(tokenizer.nextToken()));
     		    list[i].setTestScore(3,Double.parseDouble(tokenizer.nextToken()));
     			list[i].calculateAverage();
     			line = inFile.readLine();
     		 }		
     		  String name;
     		 String name1;
     		 int n;
     		 Student list1 = new Student();
     		  
     	  
     		 for (int i = 1; i < 10; ++i)
     		 {
     			name =  list[i].getLastName();
     			for (int x = 0; x < 10; ++x)
     			{
     			   name1 = list[x].getLastName();
     			   list1 = list[x];
     			   n =name1.compareTo(name);
     			   
     			   if (n > 0)
     			   {
     				  list1 = list[x];
     				  list[x] =  list[i];
     				  list[i]  = list1;
     			   }
     			}
     		 }		
     	  
     		 for ( int k =0; k < 10; k++)	   
     			System.out.println(list[k].toString());
     	  }
        
        }
     

    Here is the class file:
    Code:
     public class Student
        {
     		private String fname; 
     	  private String lname;
     	  private double test1;
     		private double test2;
     		private double test3;
     		private double average;
     		
     		public Student ()
     		{
     			fname = null;
     			lname = null;
     			test1 = 0;
     			test2 = 0;
     			test3 = 0;
     			calculateAverage();
     		}
     			
     		public Student (String fname, String lname, double t1, double t2, double t3)
     		{
     			this.fname = fname;
     			this.lname = lname;
     			test1 = t1;
     			test2 = t2;
     			test3 = t3;
     		   calculateAverage();	
     		}
     				 
     		public void setTestScore (int testNumber, double test)
     		{
     			if (testNumber == 1)
     				test1 = test;
     			else if (testNumber == 2)
     				test2 = test;
     			else test3 = test;
     		}
     		
     		public double getTestScore (int testNumber)
     		{
     			if(testNumber == 1)
     			return test1;
     			
     			else if(testNumber == 2)
     			return test2;
     			
     			else return test3;
     		}
     		
     		public void setFirstName (String fname)
     		{
     		this.fname = fname;
     		}
     		
     		public void setLastName (String lname)
     		{
     		this.lname = lname;
     		}
     		public String getFirstName ()
     		{ 
     			return fname;
     		}
     		
     		public String getLastName ()
     		{
     			return lname;
     		}
     		
     		public void calculateAverage ()
     		{
     			average =(test1+test2+test3)/3; 
     		}
     		
     		public double getAverage()
     		{
     			return average;
     		}
     

    And this the the text file that im trying to read from.
    Code:
     John Smith 100 90 80
     Kobe Bryant 100 100 100
     Bruce Lee 90 85 75
     Paul Jones 99 88.5 100
     Laura Johnshon 90.5 88 75
     Mike Adams 80 90 100
     Ron Masters 78 89 66
     Edward Little 78 88 70
     Mark Twain 100 70 60
     George Bush 90 70 80
     
     
  2. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    dont have time to make sure it works, but try changing
    Code:
      		 for (int i = 0; i< 10; i++)
     		 {
     			list[i] = new Student();
     		 
     				 tokenizer = new StringTokenizer(line);
     		 
     			list[i].setFirstName(tokenizer.nextToken());
     			list[i].setLastName(tokenizer.nextToken());
     			 list[i].setTestScore(1,Double.parseDouble(tokenizer.nextToken()));
     		    list[i].setTestScore(2,Double.parseDouble(tokenizer.nextToken()));
     		    list[i].setTestScore(3,Double.parseDouble(tokenizer.nextToken()));
     			list[i].calculateAverage();
     			line = inFile.readLine();
     		 }	
    
    to

    Code:
                     tokenizer = new StringTokenizer(line);
     		 while(tokenizer.hasMoreTokens())
     		 {
     			list[i] = new Student();
     			list[i].setFirstName(tokenizer.nextToken());
     			list[i].setLastName(tokenizer.nextToken());
     			 list[i].setTestScore(1,Double.parseDouble(tokenizer.nextToken()));
     		    list[i].setTestScore(2,Double.parseDouble(tokenizer.nextToken()));
     		    list[i].setTestScore(3,Double.parseDouble(tokenizer.nextToken()));
     			list[i].calculateAverage();
     			line = inFile.readLine();
     		 }
     
  3. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    tokenizers have been depricated, use .split() instead. much cleaner results.
     

Share This Page