• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Error in out

Im  trying to execute the value of P, what am I doing wrong?


class Examplechapter7ques9
{
public static void main(String[] args)
{

int x = 7;
int p=10;
switch (x)
{
case 1: p=15;break;
case 2: p=25;break;
case 3: p=30;break;
case 4: p=35;break;
case 5: p=40;break;
System.out.println(" "+p  );
}    
}
}
30-april.JPG
0
waltbaby315
Asked:
waltbaby315
1 Solution
 
mrjoltcolaCommented:
switch(x) means the case statement uses x for its value. Your cases don't include 7 (the value of x) so you need to put a "default" case, or add 7 as a case. Use a default anyway, for best practice.
0
 
waltbaby315Author Commented:
can you give me an example that?
0
 
cmalakarCommented:
after break, you cannot put any statements.
class Examplechapter7ques9 
{
public static void main(String[] args)
{

int x = 7;
int p=10;
switch (x)
{
case 1: p=15;break;
case 2: p=25;break;
case 3: p=30;break;
case 4: p=35;break;
case 5: p=40;break;
}
System.out.println(" "+p  );     
}
}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cmalakarCommented:
x values doesnot match with any case values (1, 2,3,4 or 5).

You can either write a case with matching value.. ie., 7 or write default.

as shown.
class Examplechapter7ques9 
{
public static void main(String[] args)
{

int x = 7;
int p=10;
switch (x)
{
case 1: p=15;break;
case 2: p=25;break;
case 3: p=30;break;
case 4: p=35;break;
case 5: p=40;break;
case 6: p=45;break;
case 7: p=50;break;  //matching 7 case
default: p=100;  //default.. will be executed if none of the case values matches with x.
}
System.out.println(" "+p  );     
}
}

Open in new window

0
 
printnix63Commented:
javac -cp . Examplechapter7ques9.java
javac -cp . Examplechapter7ques9.java
Process started >>>
<<< Process finished.
================ READY ================
java -cp . Examplechapter7ques9
java -cp . Examplechapter7ques9
Process started >>>
Wrong Value
 10
<<< Process finished.
================ READY ================

As said above, you will have to catch the case, where none of above will take.
class Examplechapter7ques9
{
	public static void main(String[] args)
	{

		int x = 7;
		int p=10;
		switch (x)
		{
			case 1: p=15;break;
			case 2: p=25;break;
			case 3: p=30;break;
			case 4: p=35;break;
			case 5: p=40;break;
			default: System.out.println("Wrong Value");
			System.out.println(" "+p  );
		}    
	}
}

Open in new window

0
 
printnix63Commented:
.. ok, cmalakar was quicker and even took care to place the System out outside the switch ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now