• Java programming if to switch

    GloriaFine Member

    I’m trying to figure out how to make my java code from IFs to Switch. Can anyone help me?

    I’m trying to figure out how to put these into switch statements,

    if (choice.equals("C"))
    else if (choice.equals("S"))
    else if (choice.equals("R"))
    

    Here’s my code,

    import javax.swing.JOptionPane;
    import java.io.*;
    import java.util.Scanner;
    
    public class Area {
    
    public static void main(String args[]) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    
    Scanner scan = new Scanner (System.in);
    
    String choice;
    String circle = "C";
    String square = "S";
    String rectangle = "R";
    int height = 0;
    int width = 0;
    int side = 0;
    
    choice = JOptionPane.showInputDialog(null, "Please Enter Your Area Choice of C, S, or R");
    
    if (choice.equals("C"))
    {
    System.out.print("Enter the radius of the circle: ");
    double radius = Double.parseDouble(reader.readLine());
    double area = Math.PI * radius * radius;
    System.out.println("Area of the circle is: " +Math.round(area));
    }
    
    else if (choice.equals("S"))
    {
    System.out.print("Enter the square's length: ");
    side = scan.nextInt ();
    double area = side * side;
    System.out.println("Area of the Square is: " +Math.round(area));
    }
    
    else if (choice.equals("R"))
    {
    System.out.print("Enter the rectangle's height: ");
    height = scan.nextInt ();
    System.out.print("Enter the rectangle's width: ");
    width = scan.nextInt ();
    double area = height * width;
    System.out.println("Area of square is: " +Math.round(area));
    }
    
    else
    {
    System.out.println("Incorrect value");
    }
    
    } // end main
    
    } // end class
    
  • Adan Member
    switch (choice) {
    case 'C': System.out.print("Enter the radius of the circle: ");
    double radius = Double.parseDouble(scan.nextLine());
    double area = Math.PI * radius * radius;
    System.out.println("Area of the circle is: " +Math.round(area));
    break;
    case 'S': System.out.print("Enter the square's length: ");
    side = scan.nextInt ();
    double area = side * side;
    System.out.println("Area of the Square is: " +Math.round(area));
    break;
    case 'R': System.out.print("Enter the rectangle's height: ");
    height = scan.nextInt ();
    System.out.print("Enter the rectangle's width: ");
    width = scan.nextInt ();
    double area = height * width;
    System.out.println("Area of square is: " +Math.round(area));
    break;
    default: System.out.println("Incorrect value");
    break;
    }
    

    Make sure to change “String choice;” to “char choice”
    The reason you use a ‘ ‘ instead of ” ” is simply because you check for chars. Checking for chars must be done with ‘ ‘.

    If you want to keep it a String, change every ‘ in your switch to ”

    Also: I saw in your code this: reader.readLine(). I changed this to scan.nextLine() simply because I prefer to work with Scanner. It’s your choice though, you can re-edit it if you like.

  • SapnaVishwas Member

    In C++ and C# we just put the name of the variable like this:

    var x;
    switch(x)
    {
    case 1: //in case the user has assigned the value 1 to the X variable
    //code goes here
    
    break;
    
    case 2: ....
    
    }
    

    (you change the “case ______” and add whatever you’d like, as long as it’s the same type of the variable declared)

  • Amit Member

    If i were to change the program so it loops back around to put another letter (choice) do I use continue ? I also need to figure out how to end the loop if I were to put a different letter besides the 3 options.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.