Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Switch

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Switch

    Hey guys working on a simple switch program but not exactly a 100% if i can convert my what was a if statement into a switch or not. Any help would be awesome Thanks!

    import java.util.Scanner;
     
    public class math
    {
      public static void main(String[] args)
    {
     
     
    Scanner input = new Scanner(System.in);
     
    System.out.println(" Please select one of these options.");    
    System.out.println(" 1. Press A to add three numbers ");
    System.out.println(" 2. Press S to subtract two numbers ");
    System.out.println(" 3. Press M to multiply by three numbers ");
    System.out.println(" 4. Press D to divide by two numbers ");
    System.out.println(" 5. Press O to modulus two numbers ");
    System.out.println(" 6. Press E to quit the program ");
    System.out.println(" ");
     
    int option= input.nextInt();
     
     
        switch (option)
        {
        case 'A': {
          addition (); }
        break;
          case 'S': {
          subtract (); }
          break;
        case 'M': {
          multiply (); }
        break;
          case 'D': {
          divide (); }
          break;
          case 'O': {
          modulus (); }
          break;
          case 'E': {
          quit ();}
          break;
          default: 
     
          System.out.println("Not a option"); }
     
        }
     
     
        public static void addition(){
                 Scanner input = new Scanner( System.in );
                 float number1;
                 float number2;
                 float number3;
                 float sum;
     
          System.out.printf( "Enter first integer: " );
          number1 = input.nextInt();
          System.out.printf( "Enter second integer: " );
          number2 = input.nextInt();
          System.out.printf( "Enter Third integer: " );
          number3 = input.nextInt();
     
                 sum = number1 + number2 + number3;
                 System.out.printf( "Sum equals %f\n", sum);
        }
        public static void subtract(){
                 Scanner input = new Scanner( System.in );
                 float number1;
                 float number2;
                 float difference;
     
          System.out.printf( "Enter number to be subtracted: " );
          number1 = input.nextInt();
          System.out.printf( "Enter integer to subtract: " );
          number2 = input.nextInt();
     
     
                 difference = number1 - number2;
                 System.out.printf( "The difference is %f\n", difference);
     
        }
        public static void multiply(){
                Scanner input = new Scanner( System.in );
                 float number1;
                 float number2;
                 float number3;
                 float product;
     
          System.out.printf( "Enter first integer to multiply: " );
          number1 = input.nextInt();
          System.out.printf( "Enter second integer: " );
          number2 = input.nextInt();
          System.out.printf( "Enter Third integer: " );
          number3 = input.nextInt();
     
          product = number1 * number2 * number3;
          System.out.printf( "The product is %f\n", product);
     
        }
     
        public static void divide(){
              Scanner input = new Scanner( System.in );
               float number1;
               float number2;
               float dividen;
     
          System.out.printf( "Enter first integer: " );
          number1 = input.nextInt();
          System.out.printf( "Enter second integer: " );
          number2 = input.nextInt();
     
          dividen = number1 / number2;
          System.out.printf( "The dividen equals %f\n", dividen);
     
        }
     
        public static void modulus(){
             Scanner input = new Scanner ( System.in );
              float number1;
              float number2;
              float sum;
     
            System.out.printf( "Enter first integer: " );
            number1 = input.nextInt();
            System.out.printf( "Enter second ineger: " );
            number2 = input.nextInt();
     
            sum = number1 % number2;
            System.out.printf( "The answer equals %f\n", sum);
     
        }
     
        public static void quit(){
          System.out.printf( "Thank you for trying to use the program.");
          return; 
     
        }
     
      }
    Last edited by KillerToFu; March 21st, 2013 at 06:24 PM. Reason: changed quote to code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch

    Can you explain what the problem is?

    The message printed for the default: should include the invalid option's value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    Sorry that's a good point didn't really specify what the problem was. The program reads the input but once it gets past that i can't get the program to start the lower operations. I'm using Dr.java this is the error
    java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at question4.main(question4.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch

    java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at question4.main(question4.java:21)
    The call to nextInt() at line 21 is trying to read an int value from the keyboard but the user is entering something that is not a numeric value. Use the next() or nextLine() method if you want to read a String not a number.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    KillerToFu (March 21st, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    Thank you, this is kind of making me feel like i may not be able to go about it in this way. It makes my option not work in the string anymore. Says that i can not make option a variable, would i have to convert a Int to a String which i have read is not a simple solution, or is there another way?

  7. #6
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Switch

    To convert an int to a String just use Integer.parseInt(number); (Integer (Java Platform SE 7 ))

    Try to be consistent. Nearly all input from GUI's are strings first and then converted.

  8. The Following User Says Thank You to Starstreak For This Useful Post:

    KillerToFu (March 21st, 2013)

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch

    To convert an int to a String just use Integer.parseInt(number)
    Wrong. The parseInt() method converts a String to an int.
    Read the String class's API doc for methods to convert an int to a String.

    With Java 1.7 switch statements will work with Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Switch

    Thanks for locating my error. If you read the rest of my reply, you will see that I'm suggesting he start with a string, and then convert since Java GUIs take this approach.

  11. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    Thanks a lot Norm really helped me figure that out. Was way easier then i was trying to make it, forgot to come back and thank you guys.


    This is what i ended up with.
    Scanner input = new Scanner( System.in );
    String aString = input.next();
    char choice = aString.charAt(0);


    switch (choice)
    {
    case 'A': {
    addition (); }

Similar Threads

  1. Switch problem
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2013, 06:22 AM
  2. what switch?
    By kingzeze in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 26th, 2013, 11:09 AM
  3. Switch in a For loop
    By Levica in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 7th, 2012, 02:21 PM
  4. Switch
    By hiepa in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 19th, 2012, 06:21 PM
  5. Help with switch statements
    By suxen in forum Loops & Control Statements
    Replies: 4
    Last Post: February 15th, 2011, 04:55 AM