So i'm working on a java program where I ask for a file name, read it into an array. My problem is that im using a string tokenizer to delimit my commas and that put the student information into an array object.
I have 10 arrays that are supposed to be filled with first and last name and student id

my problem is that I keep getting an error code with my string tokenizer

import java.util.StringTokenizer;
import java.io.*;
import java.util.Scanner;

public class StudentD

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

    {

         

        Scanner in = new Scanner(System.in);
        System.out.println("What is the File Name");
        String file = in.nextLine();

         

        File inputFile= new File(file);

        Scanner input= new Scanner(inputFile);

        String fName;
        String lName;
        String major;
        int idNumber;
        int counter=0;

         

        Student [] info = new Student [10];

         
        while(input.hasNext())

        {

         

            String line =input.nextLine(); 

            StringTokenizer strTokenizer = new StringTokenizer(line, ";" );

          while(strTokenizer.hasMoreTokens())

            {

                fName= strTokenizer.nextToken();

             

            }

             

            while(strTokenizer.hasMoreTokens())

            {

                lName= strTokenizer.nextToken();

            }

            while(strTokenizer.hasMoreTokens())

            {

                major= strTokenizer.nextToken();

            }
            while(strTokenizer.hasMoreTokens())

            {
                idNumber= Integer.parseInt(strTokenizer.nextToken());
            }
             

        }

Recommended Answers

All 8 Replies

Can you post the error or the complete code including the Student class?

Lines 45-55 you have a while loop that loops as long as there are any tokens to be parsed, and stores them in fName, each overwriting the previous value. Then it drops out of that loop and never executes any of the following while loops because you have already "processed" all the tokens.
ps Do not waste everyone's time saying "I keep getting an error code". Always quote the exact complete error message, including the line where it happened.

Here is my student utility class

public class Student 
{
		String fName; 
		String lName; 
		String major; 
		int idNumber; 
		
		public Student(String fName, String lName, String major, int idNumber) 
		{
			this.fName=fName; 
			this.lName=lName; 
			this.major=major; 
			this.idNumber= idNumber; 
		
		}
		
		public String getfName()
		{
			return fName;
		}
		public String getlName()
		{
			return lName; 
		}
		public String getMajor()
		{
			return major; 
		}
		public int getId()
		{
			return idNumber; 
		}



}

I fixed a little where now I dont get the error code, but now my problem is that it doesn't read my text file into my array.

import java.util.StringTokenizer; 
import java.io.*; 
import java.util.Scanner;


public class StudentD
{
	public static void main(String[] args) throws IOException
	{
		
		Student [] info = new Student [10];
		
		Scanner in = new Scanner(System.in); 
		System.out.println("What is the File Name"); 
		String file = in.nextLine(); 
		
		File inputFile= new File(file); 
		Scanner input= new Scanner(inputFile); 
		
		String fName; 
		String lName; 
		String major; 
		int idNumber; 
		int i=0; 
	
		while(input.hasNext())

        {

         

            String line =input.nextLine(); 

            StringTokenizer strTokenizer = new StringTokenizer(line, ";" );

          while(strTokenizer.hasMoreTokens())

            {

                fName= strTokenizer.nextToken();

             

            }

             

            while(strTokenizer.hasMoreTokens())

            {

                lName= strTokenizer.nextToken();

            }

            while(strTokenizer.hasMoreTokens())

            {

                major= strTokenizer.nextToken();

            }
            while(strTokenizer.hasMoreTokens())

            {
                idNumber= Integer.parseInt(strTokenizer.nextToken());
            }
				
			}
			
		}	
        
		
		
		

		
			
			
	}

it doesn't read my text file into my array

What do you do with the data you read from the text file?

Your first while loop will read ALL of the tokens into the one variable.
To see what is happening add a println in the loop to show the value assigned to the variable. For example for the first loop:
System.out.println("fName=" + fName); // show what was assigned to fName
Add the same type of println for all of the loops to see what is read into each variable.

You still have exactly the same mistake that I described in my first post. You haven't done a single thing about it. I'm wasting my time here. Good luck Norm
Bye
J

A suggestion: Change the while to an if

I change the while loops to if and else statements and it reads the first line but than it stops and doesn't read the other 9 lines. Also it doesn't tokenize the commas.

import java.util.StringTokenizer; 
import java.io.*; 
import java.util.Scanner;


public class StudentD
{
	public static void main(String[] args) throws IOException
	{
		
		Student [] info = new Student [10];
		
		Scanner in = new Scanner(System.in); 
		System.out.println("What is the File Name"); 
		String file = in.nextLine(); 
		
		File inputFile= new File(file); 
		Scanner input= new Scanner(inputFile); 
		
		String fName; 
		String lName; 
		String major; 
		int idNumber; 
		int i=0; 
	
		while(input.hasNextLine())

        {

      		String line =input.nextLine(); 

            StringTokenizer strTokenizer = new StringTokenizer(line, ";" );
				fName= strTokenizer.nextToken();
				System.out.println("fName=" + fName);

          if(strTokenizer.hasMoreTokens())
				 {
					lName= strTokenizer.nextToken();
					 System.out.println("lName=" + lName);

            }
				else
             if(strTokenizer.hasMoreTokens())

            {

                major= strTokenizer.nextToken();
					 System.out.println("major=" + major);

            }
				else
				if(strTokenizer.hasMoreTokens())

            {
                idNumber= Integer.parseInt(strTokenizer.nextToken());
					 System.out.println("id number=" + idNumber);
            }
			
			

			}

			
		}
			
        
}

t stops and doesn't read the other 9 lines

How do you know what it does? You don't have enough printlns to show what is happening.

What prints out when you execute this code?

Add a println at line 30 to show the value of line
Add an ending else clause at line 58 that prints out "none of the above"

Run the program and copy the console here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.