Help with "switch" (I think)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sudz28
    New Member
    • Nov 2006
    • 3

    #1

    Help with "switch" (I think)

    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.Strin gIndexOutOfBoun dsException: String index out of range: 0
    at java.lang.Strin g.charAt(Unknow n Source)
    at Project4.main(P roject4.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.prin t("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.spl it(" ");
    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(Firstna mepart, Lastnamepart, Gpapart, Creditpart, Idnumberpart);
    students.add(ne wStudent);
    System.out.prin tln("Contents of this file are: \n" + newStudent);
    } // end while
    goodFile = true;
    } //end try
    catch (IOException ie)
    {
    System.out.prin tln("The file you entered was not found.");
    System.out.prin t("Please try again. \n\n");
    } // end catch
    } // end while (!goodfile)
    System.out.prin t("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.prin t("Enter first name of student: ");
    Firstnamepart = stdin.nextLine( );
    System.out.prin t("Enter last name of student: ");
    Lastnamepart = stdin.nextLine( );
    System.out.prin t("Enter students GPA: ");
    Gpapart = stdin.nextDoubl e();
    System.out.prin t("Enter students credits: ");
    Creditpart = stdin.nextInt() ;
    System.out.prin t("Enter students ID number (SSN): ");
    Idnumberpart = stdin.nextInt() ;
    Student newStudent = new Student(Firstna mepart, Lastnamepart, Gpapart, Creditpart, Idnumberpart);
    students.add(ne wStudent);
    for (int i = 0; i < students.size() ; i++)
    System.out.prin tln("\nStudent " + i + "\n" + students.get(i) );
    break;

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

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

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

    case 'q':
    done = true;
    break;

    default:
    System.out.prin tln("Invalid Response");
    break;
    } // end switch
    } // end while (!done)
    } // end main
    } // end class
Working...