Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

1 error in my code 3rd last bracket

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all can someone see my mistake with my 3rd last bracket in my code getting an error.

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "while ( Expression ) ;" to complete DoStatement

at StudentMenu.main(StudentMenu.java:91)

Here is my code


import java.util.ArrayList;
import java.util.Scanner;

public class StudentMenu
{
public static void main ( String[] args )
{
ArrayList<Student> studentlist = new ArrayList<Student>();
Student nextStudent;
int select;
String fName;
String sName;
String grade;
int mark;

do
{
System.out.println ( "Please choose and option:[1-4].\n" +
"1 - Add a Student.\n" +
"2 - Delete a Student.\n" +
"3 - List all Students.\n" +
"4 - Exit." );

Scanner keyboard = new Scanner ( System.in );
select = keyboard.nextInt();

switch ( select )
{
case 1:
System.out.print ( "Please enter no of students " );
int numstudents = keyboard.nextInt();
keyboard.nextLine();
for ( int i = 0; i < numstudents; i++ )
{
System.out.print ( "Please enter students first name: " );
fName = keyboard.next();
System.out.println();
System.out.print ( "Please enter students surname: " );
sName = keyboard.next();
System.out.println();
System.out.print ( "Please enter your exam mark: " );
mark = keyboard.nextInt();
System.out.println();
nextStudent = new Student ( fName, sName, mark );
studentlist.add ( nextStudent );
}
break;

case 2:
{
System.out.print ( "Please enter student you want to delete first name: " );
fName = keyboard.next();
System.out.println();
System.out.print ( "Please enter students surname: " );
sName = keyboard.next();
System.out.println();
for ( Student s : studentlist )
{
if ( ( s.getFname().equals ( fName ) ) && ( s.getSname().equals ( sName ) ) )
{
studentlist.remove ( s );
}
}
}
break;

case 3:
{
for ( Student s : studentlist )
{
System.out.println ( s );
}
}
break;

case 4:
{
System.out.println ( "Goodbye" );
}
break;

{
Default:
{
System.out.println ( "Invalid Selection, Plaese try again" );
}
break;
}
while ( select != 4 );
}
}

} // end method main()

} // end class StudentMenu
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Think you have got a little nuts with brackets { } :-)

I removed a few and am now getting a complaint about not finding <Student> (I assume this is defined elsewhere).

 
Des Robin
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... oh and if you want to keep your formatting for code, the edit box has a 'Code' button (next to the Java dropdown). Simply select the code and click the button and it will insert some tags to preserve the formatting.
 
Ken O'Sullivan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Des Robin wrote:... oh and if you want to keep your formatting for code, the edit box has a 'Code' button (next to the Java dropdown). Simply select the code and click the button and it will insert some tags to preserve the formatting.



Ok I have student in a another student class but I am now getting no errors in the code but when I compile and run it I get this error
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at StudentMenu.main(StudentMenu.java:31)

 
Des Robin
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ken

Your errors are most likely related to two things:

1. You are using nextInt() without checking if the next item actually is an int (I'm making some assumptions here as I don't actually know what your input values are). Scanner provides the hasNextInt()/hasNext() for this reason.
2. I haven't personally used Scanner much but I seem to recall reading that it has issues with certain white space items like the new line character.

Hope this helps.
 
Ken O'Sullivan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya you are right I dont have any errors now in the code but after it runs I get an error when running it,problem with the scanner and int.

Please choose and option:[1-4].
1 - Add a Student.
2 - Delete a Student.
3 - List all Students.
4 - Exit.
1
Please enter no of students lkmskfn
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at StudentMenu.main(StudentMenu.java:31)

 
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken O'Sullivan wrote:Ya you are right I dont have any errors now in the code but after it runs I get an error when running it,problem with the scanner and int.



This is because your code doesn't handle the error condition. You need to either (1) read the token in (not as an int), confirm it is an int, and parse it; If not, it should try again, or (2) when you get the exception, read the junk token and toss it, and try again.

Henry
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a helpful article that tells you how to work with Scanner(s)/User Input. I think it's quite good.
 
Marshal
Posts: 79082
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79082
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just looking at the code, I can see another problem: you are putting far too much code in the main method. Any method which occupies more than ½ a page is probably too long. A method which occupies a whole page is definitely too long. What you can do in that switch is to call a method for each (valid) menu element.

Search my posts for utility class and Scanner hasNextInt and you will find posts telling people to search my posts for utility class and Scanner hasNextInt how to write utility classes. I suggest you should write yourself a utility class so you can call KeyboardInputs.getInt() and it will skip incorrect input like lkmskfn
The problem mentioned does not affect whitespace. It is about nextLine not reading the next line, and it is described here.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic