5 Replies - 4848 Views - Last Post: 18 September 2009 - 11:59 PM Rate Topic: -----

#1 needjavahelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-September 09

unreachable code in java

Post icon  Posted 17 September 2009 - 09:35 AM

Here is the error. Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code. Below is the code I started to begin and am stuck.

at Sales.main(Sales.java:32)

import java.util.Scanner;
public class Sales
{
public static void main(String args[])
{
//create Scanner to obtain input from command window
Scanner input = new Scanner (System.in);

int product,quantity;
double total=0.00;
System.out.print( "Enter number (1-5)(0 to stop:)");
product=input.nextInt();


double product_one;
product_one= 2.98;
double product_two;
product_two= 4.50;
double product_three;
product_three= 9.98;
double product_four;
product_four= 4.49;
double product_five;
product_five= 6.87;
while(product !=0){
System.out.print("Enter quantity: ");
quantity=input.nextInt();
switch( product ){}
product_one:total+=quantity*2.98; // line 30 In Eclipse This line says lable product_one is never explicitly referenced
break;
product_two:total+=quantity*4.50; //This is the line 32 that is causing error I mentioned above.
break;
product_three:total+=quantity*9.98;
break;
product_four:total+=quantity*4.49;
break;
product_five:total+=quantity*6.87;
break;




}}} //Thanks for helping me with this problem in advance this forum is awesome.

Is This A Good Question/Topic? 0
  • +

Replies To: unreachable code in java

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: unreachable code in java

Posted 17 September 2009 - 09:40 AM

can you please edit your post and add the code tags first

anyways, your switch statement is the problem
switch( product ){
case 1 : total+=quantity*2.98; // line 30 In Eclipse This line says lable product_one is never explicitly referenced
break;
case 2 : total+=quantity*4.50; //This is the line 32 that is causing error I mentioned above.
break;
case 3: total+=quantity*9.98;
break;
case 4 : total+=quantity*4.49;
break;
case 5 : total+=quantity*6.87;
break;
}


also you dont really have to put a break after the last case

This post has been edited by mostyfriedman: 17 September 2009 - 09:41 AM

Was This Post Helpful? 1
  • +
  • -

#3 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: unreachable code in java

Posted 17 September 2009 - 02:12 PM

Ya the Switch block was closed immediately after opening :)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: unreachable code in java

Posted 17 September 2009 - 02:27 PM

Just wanted to add, I don't know if you are familiar with this, but you can declare and initialize variables in one step, like so:

double product_one = 2.98;



It saves you a little work, and will make your code cleaner, especially as you work on more complex programs.
Was This Post Helpful? 1
  • +
  • -

#5 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: unreachable code in java

Posted 17 September 2009 - 02:35 PM

View Postmacosxnerd101, on 18 Sep, 2009 - 01:57 AM, said:

It saves you a little work, and will make your code cleaner, especially as you work on more complex programs.


If you think about Optimization then he can actually Remove all that product_xxx declarations.. He doesn't need to use it now :P
Was This Post Helpful? 0
  • +
  • -

#6 needjavahelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-September 09

Re: unreachable code in java

Posted 18 September 2009 - 11:59 PM

View Postmostyfriedman, on 17 Sep, 2009 - 08:40 AM, said:

can you please edit your post and add the code tags first

anyways, your switch statement is the problem
switch( product ){
case 1 : total+=quantity*2.98; // line 30 In Eclipse This line says lable product_one is never explicitly referenced
break;
case 2 : total+=quantity*4.50; //This is the line 32 that is causing error I mentioned above.
break;
case 3: total+=quantity*9.98;
break;
case 4 : total+=quantity*4.49;
break;
case 5 : total+=quantity*6.87;
break;
}


also you dont really have to put a break after the last case

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1