By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Help with "switch" (I think)

P: 3
Greetings! I am attempting to write a program that will allow a user to manipulate data read from a file, and am obviously in no way near finished. However, one problem I'm having that I don't understand is with my switch subroutine. In the below code, if I select "d" or anything that is an invalid response, it loops correctly back to the menu. However, if I attempt to insert (i), find (f), or delete (d) it loops back to the menu *but* immeidately crashes with the error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at Project4.main(Project4.java:62)

Can anyone tell me why I'm getting this error? It seems to be immediately assuming some sort of input on the part of the user before anything is typed, and I don't understand why.
Thanks!

public class Project4
{

// Begin Main

public static void main(String[] args) throws IOException
{
Scanner stdin = new Scanner(System.in);
String choice, response = null;
ArrayList students = new ArrayList();
boolean done = false;
boolean goodFile = false;
String Firstnamepart;
String Lastnamepart;
double Gpapart;
int Creditpart;
int Idnumberpart;
File input = null;
while (!done)
{
while (!goodFile)
{
System.out.print("Enter input file name: ");
try
{
String name = stdin.nextLine();
input = new File(name);
Scanner fileIn = new Scanner(input);
while (fileIn.hasNext())
{
String currentLine = fileIn.nextLine();
String[] parts = currentLine.split(" ");
Firstnamepart = parts[0];
Lastnamepart = parts[1];
Gpapart = new Double(parts[2]).doubleValue();
Creditpart = new Integer(parts[3]).intValue();
Idnumberpart = new Integer(parts[4]).intValue();
Student newStudent = new Student(Firstnamepart, Lastnamepart, Gpapart, Creditpart, Idnumberpart);
students.add(newStudent);
System.out.println("Contents of this file are: \n" + newStudent);
} // end while
goodFile = true;
} //end try
catch (IOException ie)
{
System.out.println("The file you entered was not found.");
System.out.print("Please try again. \n\n");
} // end catch
} // end while (!goodfile)
System.out.print("Would you like to: insert student(i), remove student(r), find student(f), display list(d) or quit(q): ");
choice = stdin.nextLine();
switch (choice.charAt(0))
{
case 'i':
System.out.print("Enter first name of student: ");
Firstnamepart = stdin.nextLine();
System.out.print("Enter last name of student: ");
Lastnamepart = stdin.nextLine();
System.out.print("Enter students GPA: ");
Gpapart = stdin.nextDouble();
System.out.print("Enter students credits: ");
Creditpart = stdin.nextInt();
System.out.print("Enter students ID number (SSN): ");
Idnumberpart = stdin.nextInt();
Student newStudent = new Student(Firstnamepart, Lastnamepart, Gpapart, Creditpart, Idnumberpart);
students.add(newStudent);
for (int i = 0; i < students.size(); i++)
System.out.println("\nStudent " + i + "\n" + students.get(i));
break;

case 'r':
System.out.print("Enter ID number of student to be removed: ");
Idnumberpart = stdin.nextInt();
// try
// {
// for (int i = 0; i < students.size(); i++)
// students.get(Idnumberpart);
// newStudent = students.contains(i);
// students.remove(newStudent);
// } // end try
// catch (NotFound exception)
// {
// System.out.println("Student with ID number " + Idnumberpart + "was not found.");
// } // end catch
break;

case 'f':
System.out.println("Enter ID number of student to find: ");
Idnumberpart = stdin.nextInt();
break;

case 'd':
System.out.println("Choice = " + choice);
System.out.println("List is now:");
for (int i = 0; i < students.size(); i++)
System.out.println("\nStudent " + i + "\n" + students.get(i));
break;

case 'q':
done = true;
break;

default:
System.out.println("Invalid Response");
break;
} // end switch
} // end while (!done)
} // end main
} // end class
Nov 20 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.