Question

Find the error in the following java code and explain how to correct it (val is...

Find the error in the following java code and explain how to correct it (val is a variable of type int):

 switch (val) {
      case 1:  
           System.out.println ("The number is 1");
      case 2: 
           System.out.println ("The number is 2");
     default:
           System.out.println ("The number is not 1 or 2");
           break;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In you java code,case 1 and case 2 does not have break statement,therefore it results in executing all three printing statements.

Below is the corrected code:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   //sample value of variable val i.e 1.
   Scanner scn = new Scanner(System.in);
   //taking user input and storing in val.
   int val = scn.nextInt();
   //checking using switch case
   //whether val is 1 or 2 or other number.
       switch (val) {
case 1:
System.out.println ("The number is 1");
//included break statement for case 1.
break;
case 2:
System.out.println ("The number is 2");
//included break statement for case 2.
break;
default:
System.out.println ("The number is not 1 or 2");
break;
       }
   }
}

Refer to the screenshot attached below to better understand the code and indentation:


Output:
when val = 1.

when val = 2.

when val is other than 1 or 2.

If this answer helps you then please upvote,
for further queries comment below.


Thank you.

Add a comment
Know the answer?
Add Answer to:
Find the error in the following java code and explain how to correct it (val is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 26. Select the correct text in the passage. Jamie wrote the following code to find a...

    26. Select the correct text in the passage. Jamie wrote the following code to find a maximum number of a pair of numbers, but is receiving an error. In which line does his error appear? int a, b, val; final int NUMVALS 10; for (int 1-1; 1 <-NUMVALS; İ + 1) { a (int) (Math.randomO 10 1); b (int) (Math.randomO 10 1); System.out.println(a); System.out.println(b); if (a > b) b-a Select... Select. int a, b, val; final int NUMVALS = 10;...

  • Pick a switch statement from the reading. Put it into a main() Java program and make...

    Pick a switch statement from the reading. Put it into a main() Java program and make it work. Attach the .java file and copyAndPaste the output into the WriteSubmissionBox The switch Statement S1.51 Multiway if-else statements can become unwieldy when you must choose from among many possible courses of action. If the choice is based on the value of an integer or character expres- sion, the switch statement can make your code easier to read. The switch statement begins with...

  • use java and write in text a. Rewrite the following code segment using if statements. Assume...

    use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...

  • Debug the following java code so that it will have given out put at the bottom....

    Debug the following java code so that it will have given out put at the bottom. Make sure to fix all syntax and logical errors. import java.util.Scanner; public class Graphs { // draws 5 histograms public void drawHistograms() Scanner input = new Scanner( System.in ); int number1 = 0; // first number int number2 = 0; // second number int number3 = 0; // third number int number4 = 0; // fourth number int number5 = 0; // fifth number...

  • need help with java questions The following snippet of code would produce what outcome? public static...

    need help with java questions The following snippet of code would produce what outcome? public static void main(String 2 [] args) { int day = 5; switch (day) { case 1: System.out.println("Monday "); case 2: System.out.println("Tuesday "); case 3: System.out.println("Wednesday "); case 4: System.out.println("Thursday "); case 5: System.out.println("Friday "); case 6: System.out.println("Saturday "); case 7: System.out.println("Sunday "); break; default: System.out.println("Invalid Day "); } } Invalid Day Friday Saturday Sunday Invalid Day Friday Friday Saturday Sunday What will be the output...

  • Please try to explain the answers as well. It will be highly appreciated. Output of the...

    Please try to explain the answers as well. It will be highly appreciated. Output of the following expression: 5 * 4 % 2 = ? Output of a loop: Example: What will be the output of the Java code (Assume all variables are properly declared.) num = 10; while (num <= 32)                 num = num + 5; System.out.println(num); What will be the output of the Java code (Assume all variables are properly declared.) public class test {   ...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • Find the errors in the following code: / / Warning! This code contains ERRORS! switch (score)...

    Find the errors in the following code: / / Warning! This code contains ERRORS! switch (score) { case (score > 90): grade = 'A'; break; case(score > 80): grade = 'B'; break; case(score > 70): grade = 'C'; break; case (score > 60): grade = 'D'; break; default: grade = 'F'; } *Submit in Java format

  • Hi. This is a prototype of Java. The following Java program was developed for prototyping a...

    Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT