1

I only need help with the input part. If the user inputs a number I need the program to read and output a case that equals the number that was input.

//This program will display the months of the year

public class MonthsOfTheYear {
    public static void main(String[] args) 
            throws java.io.IOException{

        int month;

        System.out.println("Please enter a Month Number: ");

        month = (int) System.in.read(); //Get an integer

        switch (month) {
        case 1:  System.out.println("January");
        break;
        case 2:  System.out.println("February");
        break;
        case 3:  System.out.println("March");
        break;
        case 4:  System.out.println("April");
        break;
        case 5:  System.out.println("May");
        break;
        case 6:  System.out.println("June");
        break;
        case 7:  System.out.println("July");
        break;
        case 8:  System.out.println("August");
        break;
        case 9:  System.out.println("September");
        break;
        case 10: System.out.println("October");
        break;
        case 11: System.out.println("November");
        break;
        case 12: System.out.println("December");
        break;
        default: System.out.println("Invalid Month");
        break;
        }
        System.out.println();
    }
}
2

6 Answers 6

1

Try using the Console class instead: http://docs.oracle.com/javase/6/docs/api/java/io/Console.html to obtain user input, then convert the String to a number with Integer.parseInt(String)

Also, be aware that the next version of Java (8) will support Strings in case blocks.

1
  • 1
    ... by clicking the checkbox image.
    – noahlz
    Apr 15, 2013 at 17:25
1
class vehicle
{
   int passengers;
   int fuelcap;
   int mpg;
}

import java.io.*;
class Vehicle_Demo
{

   public static void main (String args[]) throws java.io.IOException
   {
      vehicle obj1 = new vehicle();
      obj1.passengers=12;
      obj1.fuelcap=9;
      obj1.mpg=78;

      System.out.println(obj1.passengers, obj1.fuelcap, obj1.mpg);
   }
}
0

You can use Scanner to read your System.in

Scanner input = new Scanner(System.in);
month = input.nextInt();

Output:

Please enter a Month Number: 
1
January

See related

Edit:

as noahz pointed out there is a Console class that covers the same functionality. For an idea of the difference between the two, read this.

1
0

Try to use TextIO Input Functions

This is better than scanner input

You need to compile TextIO.java fist as a prerequisite so that TextIO.class must be found in the same folder.

use this input function instead:

month = TextIO.getChar();

Please let me know if you have problems on this method.

Thanks!

0

Correct me if I'm wrong, but I'm pretty sure it will still work with "system.in.read". You're switch cases do not have single quotes around the input you want to be processed. If you want case one to be run when you type '1' your case should be case '1', not case 1.

0
0

There problem here was Java have cast a character to an integer, therefore you get an ASCII value.
For instance, when you type 5 you've got 53 in ASCII. Check it taking the variable value in System.out.println();

I've tried to solve that changing month to a char type and then passing a char value to the switch control. But the problem is, getting System.in.read(); method you just can get a single character. At last you just can select between 1 to 9.

I suspect the solution is using a buffer reader. Until then, I leave you here my code:

public class MonthsOfTheYear {
    public static void main(String[] args) 
            throws java.io.IOException{

        char month;

        System.out.println("Please enter a Month Number: ");

        month = (char) System.in.read(); //Get an integer
        //System.out.println("Actual value of :" + month);

        switch (month) {
        case '1':  System.out.println("January");
        break;
        case '2':  System.out.println("February");
        break;
        case '3':  System.out.println("March");
        break;
        case '4':  System.out.println("April");
        break;
        case '5':  System.out.println("May");
        break;
        case '6':  System.out.println("June");
        break;
        case '7':  System.out.println("July");
        break;
        case '8':  System.out.println("August");
        break;
        case '9':  System.out.println("September");
        break;
        /*case '10': System.out.println("October");
        break;
        case '11': System.out.println("November");
        break;
        case '12': System.out.println("December");
        break;*/
        default: System.out.println("Invalid Month");
        break;
        }
        System.out.println();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.