Results 1 to 3 of 3
  1. #1
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default the switch statement and unreachable statement error

    Hi,
    I am writing a code for a school assignment that should keep track of the quantity of items in stock at a store, and be able to sort them by name and quantity. I used the switch statement to allow the user to navigate through the program like a menu. When I compiled the program, it gave four error messages, all for the same reason, "unreachable statement" error for the lines containing break; on lines 147, 167, 221, 242 (I will attach a screenshot of the errors). I searched the internet for an answer, but I couldn't find anything I could apply to my program. Could someone review my work and tell me what i did wrong?

    Thanks

    the switch statement and unreachable statement error-capture-com_r_us-.png

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class com_r_us
    {
    	public static void main(String[] args) throws IOException
    	{
    		//Greeting
    		System.out.println ("Welcome to Inventory Maneger");
    		System.out.println ("----------------------------");
    		System.out.println (" ");
    		
    		//create input stream
            BufferedReader in;
            in = new BufferedReader(new InputStreamReader(System.in));
    		
    		//arrays
    		String product[] = new String [4];
    		int stock[] = new int [4];
    		
    		//Loops
    		int a;
    		int i;
    		int j;
    		int temp;
    		String temp2;
    		
    		//Input Data
    		for (a = 0; a <= 4; a++)
    		{
    			//Input products
    			System.out.println ("Input product " + (a+1) + ":");
    			String product_i = in.readLine();
    			product[a] = product_i;
    			
    			//Input stocks
    			System.out.println ("Input stock of " + product_i + ":");
    			String stock_i2 = in.readLine();
    			int stock_i = Integer.valueOf(stock_i2).intValue();
    			stock[a] = stock_i;
    		}
    		
    		//menu choice
    		int menuchoice;
    		String menuchoice_i;
    		int menuchoice2;
    		String menuchoice2_i;
    		int menuchoice3;
    		String menuchoice3_i;
    		
    		//loop
    		do
    		{
    			//Main Menu
    			System.out.println ("|----------------|");
    			System.out.println ("| Main Menu      |");
    			System.out.println ("|----------------|");
    			System.out.println ("| 1 = Input Data |");
    			System.out.println ("| 2 = View Data  |");
    			System.out.println ("| 3 = Quit       |");
    			System.out.println ("|----------------|");
    			
    			//Input Choice
    			System.out.println("Input Choice:");
    			menuchoice_i = in.readLine();
    			System.out.println (" ");
    			
    			menuchoice = Integer.valueOf(menuchoice_i).intValue();
    			
    			switch (menuchoice)
                {
    				//Input Data
    				case 1:
    					for (a = 0; a <= 4; a++)
    					{
    						//Input products
    						System.out.println ("Input product " + (a+1) + ":");
    						String product_i = in.readLine();
    						product[a] = product_i;
    						
    						//Input stocks
    						System.out.println ("Input stock of " + product_i + ":");
    						String stock_i2 = in.readLine();
    						int stock_i = Integer.valueOf(stock_i2).intValue();
    						stock[a] = stock_i;
    					}
    					break;
    				
    				//View Data
    				case 2:
    					//View Menu
    					System.out.println ("|--------------------|");
    					System.out.println ("| View Menu          |");
    					System.out.println ("|--------------------|");
    					System.out.println ("| 1 = Sort by Name   |");
    					System.out.println ("| 2 = Sort by Stock  |");
    					System.out.println ("| 3 = Return to Menu |");
    					System.out.println ("|--------------------|");
    					
    					//Input choice 2
    					System.out.println("Input Choice:");
    					menuchoice2_i = in.readLine();
    					System.out.println (" ");
    					
    					menuchoice2 = Integer.valueOf(menuchoice2_i).intValue();
    					
    					switch (menuchoice2)
    					{
    						//Sort by Name
    						case 1:
    						{
    							System.out.println ("|---------------------|");
    							System.out.println ("| View Menu 2 (Name)  |");
    							System.out.println ("|---------------------|");
    							System.out.println ("| 1 = Sort Ascending  |");
    							System.out.println ("| 2 = Sort Descending |");
    							System.out.println ("|---------------------|");
    							System.out.println (" ");
    							
    							//Input choice 2
    							System.out.println("Input Choice:");
    							menuchoice3_i = in.readLine();
    							System.out.println (" ");
    							
    							menuchoice3 = Integer.valueOf(menuchoice3_i).intValue();
    							
    							switch (menuchoice3)
    							{
    								case 1:
    								//Sort Ascending
    									for (i=0; 1<3; i++)
    									{
    										for (j=0; j<3 - i; j++)
    										{
    											if (product[j+1].compareTo(product[j]) < 0)
    											{
    												temp2 = product[j];
    												product[j] = product [j+1];
    												product[j+1] = temp2;
    												
    												temp = stock[j];
    												stock[j] = stock [j+1];
    												stock[j+1] = temp;
    											}
    										}
    									}
    									break;
    								
    								case 2:
    								//Sort Descending
    									for (i=0; 1<3; i++)
    									{
    										for (j=0; j<3 - i; j++)
    										{
    											if (product[j+1].compareTo(product[j]) > 0)
    											{
    												temp2 = product[j+1];
    												product[j+1] = product [j];
    												product[j] = temp2;
    												
    												temp = stock[j+1];
    												stock[j+1] = stock [j];
    												stock[j] = temp;
    											}
    										}
    									}
    									break;
    								
    								default:
    								//Error
    									System.out.println ("Not an option...");
    									System.out.println (" ");
    									break;
    							}
    							for (a=0; a < 3; a++)
    							{
    								System.out.println (product);
    								System.out.println (stock);
    								System.out.println (" ");
    							}
    						}
    					
    						//Sort by Stock
    						case 2:
    						{
    							System.out.println ("|---------------------|");
    							System.out.println ("| View Menu 2 (Stock) |");
    							System.out.println ("|---------------------|");
    							System.out.println ("| 1 = Sort Ascending  |");
    							System.out.println ("| 2 = Sort Descending |");
    							System.out.println ("|---------------------|");
    							
    							//Input choice 2
    							System.out.println("Input Choice:");
    							menuchoice3_i = in.readLine();
    							System.out.println (" ");
    							
    							menuchoice3 = Integer.valueOf(menuchoice3_i).intValue();
    							
    							switch (menuchoice3)
    							{
    								//Sort Ascending
    								case 1:
    								{
    									for (i=0; 1<3; i++)
    									{
    										for (j=0; j<3 - i; j++)
    										{
    											if (stock[j+1] < stock[j])
    											{
    												temp2 = product[j];
    												product[j] = product [j+1];
    												product[j+1] = temp2;
    												
    												temp = stock[j];
    												stock[j] = stock [j+1];
    												stock[j+1] = temp;
    											}
    										}
    									}
    									break;
    								}
    								
    								//Sort Descending
    								case 2:
    									for (i=0; 1<3; i++)
    									{
    										for (j=0; j<3 - i; j++)
    										{
    											if (stock[j+1] > stock[j])
    											{
    												temp2 = product[j+1];
    												product[j+1] = product [j];
    												product[j] = temp2;
    												
    												temp = stock[j+1];
    												stock[j+1] = stock [j];
    												stock[j] = temp;
    											}
    										}
    									}
    									break;
    								
    								//Error
    								default:
    									System.out.println ("Not an option...");
    									System.out.println (" ");
    									break;
    							}
    						}
    						
    					break;
    					}
    				//Quit
    				case 3:
    				{
    					System.out.println ("Goodbye");
    					System.out.println (" ");
    					break;
    				}
    			}
    			
    		} while (menuchoice != 3);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: the switch statement and unreachable statement error

    Look at the for-loop condition at line #131: 1 < 3; one is always less than three so that loop never terminates, so, the statement following that loop will never be executed.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: the switch statement and unreachable statement error

    Sorry, that should have been i. Thanks for pointing that out. My error is gone now, so I can continue working. Thanks for the help.

Similar Threads

  1. Error With Portion of Switch Statement
    By Cod in forum New To Java
    Replies: 14
    Last Post: 03-24-2011, 12:25 PM
  2. Strange unreachable statement error...
    By silafirion in forum New To Java
    Replies: 5
    Last Post: 12-11-2010, 06:05 AM
  3. Unreachable statement
    By lala in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 04:32 PM
  4. Unreachable statement Pig Dice Game
    By noturn10 in forum New To Java
    Replies: 2
    Last Post: 11-22-2009, 01:36 AM
  5. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 01:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •