Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are told to use SWITCH on this program instead of IF/ELSE....this is what I h

ID: 3528334 • Letter: W

Question

We are told to use SWITCH on this program instead of IF/ELSE....this is what I have on the IF/ELSE but for some reason when I tried on switch it kept on giving me errors. import java.util.Scanner; public class Year{ public static void main( String [] args) { Scanner scan = new Scanner(System.in); System.out.println(" year enter "); String input = scan.next(); int x,y; y = 2000; if(year.length() == 2) { x = (y + Integer.parseInt(year)); System.out.println("year is " + x); } else if(year.length() == 4) { number = Integer.parseInt(year); System.out.println("year is " + x); } else System.out.println("invalid. "); } }

Explanation / Answer

/** code using case statement.also corrected few errors....now its working fine **/

import java.util.Scanner;

public class Year{

public static void main( String [] args){

Scanner scan = new Scanner(System.in);

System.out.println(" year enter ");

String year = scan.next();

int x,y; y = 2000;

switch(year.length())

{

case 2:

x = (y + Integer.parseInt(year));

System.out.println("year is " + x);

break ;

case 4:

x = Integer.parseInt(year);

System.out.println("year is " + x);

break ;

default : System.out.println("invalid. ");

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote