Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Getting 1 error in my menu driven code for students add,delete and list.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting 1 error in my menu driven code for students add,delete and list.

     
     
    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's 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)))
     
                        	//(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);
    	            }   
     
    	    }
    	        }
     
    	    }

    The error is with the 3rd last } bracket,This is what the error is saying.

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

    at StudentMenu.main(StudentMenu.java:104)


  2. #2
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    What IDE are you using?

    It seems like you need to organize your brackets. Pretty much error is saying your while ( expression ) is after the wrong bracket (not at the end of the do statement.).

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    The error message is incredibly helpful, telling you what exactly is wrong. What do you need from us?

    I suggest you indent your code properly. Your indents are currently all over the place and useless. With properly indented source code PLUS the error message. you'll see the source of the error.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    Quote Originally Posted by Rend View Post
    What IDE are you using?

    It seems like you need to organize your brackets. Pretty much error is saying your while ( expression ) is after the wrong bracket (not at the end of the do statement.).

     
     
    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);
    	            	}
     
     
    	        		}
    	    			}
     
    	    			}

    Is that better now still cant see my mistake,checked all brackets and also moved the while but it made it worse.

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    The error message is incredibly helpful, telling you what exactly is wrong. What do you need from us?

    I suggest you indent your code properly. Your indents are currently all over the place and useless. With properly indented source code PLUS the error message. you'll see the source of the error.



     
     
    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);
    	            	}
     
     
    	        		}
    	    			}
     
    	    			}

    Is that better now still cant see my mistake,checked all brackets and also moved the while but it made it worse.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    You're funny. I'm chuckling anyway. You moved the while() statement from after the close brace to below the same close brace. Did you expect that to make a difference?

    And you have an odd sense of properly indented code. I've pasted how it would be properly indented in my world. If you don't know how to do this or get help doing this, then ask. (Check out the switch() statement's default block.)
    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

    Edit: Oh, and I would reduce all lines to less than 80 columns.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    Sorry ya your right i dont know how to indent the code never did it,this 1 error is driving me around the bend,can you see my mistake because i cant for the life of me.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    Find the close brace at the end of the do{} loop and move the while() statement immediately after that brace, same line as the '}', next line, it doesn't matter. The '{' that begins the do loop is underneath the 'do'. The '}' that ends the do{} loop is lined up with the '{' that begins the do{} loop.

    What source code editor or IDE are you using?

    If you fix the default: clause, the error in while() location should be even more obvious.

  8. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    The IDE I am using is eclipse,I did what you said but now getting an error with the default case it is unreachable code?

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Unreachable code

    at StudentMenu.main(StudentMenu.java:82)

    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 );
                }
            }
            while ( select != 4 );
     
     
        } // end method main()
     
    } // end class StudentMenu

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    Using Eclipse, there's no reason to post the mess you had originally. To format your code, select all of it, or just a piece of it that you want to format, and select CTRL-i.

    To find the end of the the do{} clause, put your cursor next to the open brace, '{', after the 'do', and the ending brace, '}', will be highlighted or somehow identified depending on how you've configured your installation.

    No, you didn't do what I said, because I said, "If you fix the default: clause, the error in while() location should be even more obvious." You haven't fixed the default: clause, and there should not be two while() statements. The first one is currently being ignored, because it's an empty while() clause, but still, there shouldn't be two of them.

  10. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting 1 error in my menu driven code for students add,delete and list.

    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)

    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.nextLine();
                            System.out.println();
                            System.out.print ( "Please enter students surname: " );
                            sName  = keyboard.nextLine();
                            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" );
                    }
     
     
                    {
                        Default:
                        {
                            System.out.println ( "Invalid Selection, Plaese try again" );
                        }
                        break;
                    }
     
                }
            }
            while ( select != 4 );
     
     
        } // end method main()
     
    } // end class StudentMenu

Similar Threads

  1. [SOLVED] Code from textbook not working and I don't know why--Event-driven programming
    By BloomingNutria in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 9th, 2012, 08:58 PM
  2. help with delete method of linked list
    By sonicjr in forum Collections and Generics
    Replies: 1
    Last Post: February 18th, 2012, 08:21 PM
  3. Java App - Add, Delete, Reorder elements of the buttons
    By alibm in forum AWT / Java Swing
    Replies: 5
    Last Post: March 7th, 2011, 07:46 AM
  4. Replies: 1
    Last Post: May 12th, 2010, 08:54 AM
  5. How to delete and add elements to an array dynamically?
    By k_w_r2007 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2009, 11:31 AM