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
0 1363 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mj23 |
last post by:
I'm Italian and I'm sorry for my English!!!I'm new of this ns and so I don't
know if my question is suitable.
I have a problem: using the C++ language,the QT library and the OpenGL
library realize a CAD that can:
a) load a glut three-dimensional model
b) load a three-dimensional model in dxf format
Thank you very much
|
by: milkyway |
last post by:
Hello,
I have an HTML page that I am trying to import 2 .js file (I created)
into. These files are: row_functions.js and data_check_functions.js.
Whenever I bring the contents of the files into this HTML file, all is
OK but whenever the functions are separated (as it is now), when I run
the page, I get the following error:
Line 73, object expected.
|
by: steveee |
last post by:
Hi!
I'm building a db in access for a school project, and was wondering how to
make the value for a field the same as the number of records with the same
value in another field.
e.g.
DvdTitle = Pulp fiction
StockLev = 1
DvdTitle = Finding Nemo
|
by: Pieter Linden |
last post by:
Hi,
I think the subject line pretty much says it all...
Say I have a students-classes database and I add a twist. I want to
filter what courses a student can take by comparing the courses he has
taken to the list of prerequisites for the remaining courses.
Everything is fine if a course only has one or no prerequisite. How
do you express in SQL something like:
"Course C" requires "Course A" and "Course B"
|
by: Brian Basquille |
last post by:
Hello all,
Well, we've gotten to it: the real meaty area of my Air Hockey game..
Any help or suggestions for the following would be much appreciated.
In plain terms:
My paddle needs to be able to strike the puck and move in the direction
opposite to which it was struck (a simple collision, basically!)
| |
by: paul meaney |
last post by:
All,
myself and another developer have been staring blankly at a screen for
the past 48 hours and are wondering just what stunningly obvious thing
we are missing.
We are trying to load up 2 or more user controls dynamically by adding
to a placeholder defined in page_load. I've included the sample code
for how we are accessing one. The user controls are not rocket science
- just a few text boxes with public accessor properties. We've...
|
by: Justin |
last post by:
I am trying to do a simple update of a single row with the dataset below but
I keep getting this error:
"System.NullReferenceException: Object reference not set to an instance of
an object."
Here is the code:
Me.sqlDataAdapter1.Fill(DataSet1, "Queue")
|
by: GrandpaB |
last post by:
While writing this plea for help, I think I solved my
dilemma, but I don't know why the problem solving
statement is necessary. The inspiration for the
statement came from an undocumented VB example I found on
the web. I would be most appreciative if someone could
explain why this statement is necessary and what does it
do:
MyArt = New Art
|
by: Thelma Lubkin |
last post by:
I would like my DLookup criteria to say this: Trim(fieldX) = strVar:
myVar = _
DLookup("someField", "someTable", "Trim(fieldX) = '" & strVar & '")
I don't believe that this will work, and I won't be at a machine with
access to Access for a while, so can someone please tell me how to write
this?
thanks, --thelma
|
by: cap213 |
last post by:
This part of C always gets me...it shouldn't...but it does, so here I go:
I've looked everywhere but I can't find the answer anwhere
void Arrays (int numArray, int quantity)
{
FILE *fp;
fp = fopen("stock.dat", "rb+");
fclose(fp);
}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |