LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   switch usage in Java, programming rule/grammar (https://www.linuxquestions.org/questions/programming-9/switch-usage-in-java-programming-rule-grammar-4175468656/)

fhleung 07-05-2013 11:53 PM

switch usage in Java, programming rule/grammar
 
switch logic is available almost all types of programming language.
Code:

int type;
type = System.in.read();
switch(type) {


case 11:
switch(type2) {
case 21:
variable = "case 21";
case 22:
variable = "case 22";
break;


case 12:
variable = "case 2";
break;
}

My question is:

I would like to have DOUBLE switch(), I mean like second switch() inside each case like above code. Could you please show me a brief implementation/example ?


in the switch(type) , inside the bracket, what type of this variable can be?



Thank you.

psionl0 07-06-2013 01:18 AM

Do you mean something like this?
Code:

switch(type) {

  case 11:
      switch(type2) {
        case 21:
            variable = "case 21";
            break;
        case 22:
            variable = "case 22";
            break;
      }
      break;


  case 12:
      variable = "case 2";
      break;
}

type could be char, short, int, long or enumerated.

fhleung 07-06-2013 08:57 PM

Thank you for reply. The type variable in the switch(type) is INPUT
and the OUTPUT would be, to the console screen. So...


INPUT = switch(type) switch(type2)
OUTPUT = System.out.print(variable);


for example code to test, I have System.in.read(); to get the input from keyboard
but it's NOT work. Any idea?
Code:

type = System.in.read();

psionl0 07-06-2013 09:24 PM

Quote:

Originally Posted by fhleung (Post 4985442)
I have System.in.read(); to get the input from keyboard
but it's NOT work. Any idea?

You have to press the Enter key to get characters into the input buffer.

The standard Java console is not designed for inputting individual characters.

fhleung 07-06-2013 09:42 PM

Code:

import java.util.*;
import java.io.*;
import java.lang.*;

public class  {

public static void main(String[] args) {
                int type;
                int type2 = 21;
                String variable = null;

InputStreamReader i = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(i);

                                type = br.read();
                                //type = System.in.read();

                                switch (type) {
                               
                                case 11:
                                        switch(type2) {
                                        case 21:
                                            variable = "case 21";
                                            break;
                                        case 22:
                                            variable = "case 22";
                                            break;
                                        }
                                        break;
                                case 12:
                                        variable = "case 2";
                                        break;
                                default:
                                        variable = "case 3";
                                        break;
                                }

                                System.out.print(variable);

}
}

"You have to press the Enter key to get characters into the input buffer."
Enter pressED after something on keyboard keyin

Please run the above code. It always output default. Any idea?

psionl0 07-06-2013 11:51 PM

Try type = System.in.read();

Bear in mind that codes 11 and 12 are control characters which won't be in your input stream so you will always get the default case.

fhleung 07-08-2013 01:25 AM

"Bear in mind that codes 11 and 12 are control characters which won't be in your input stream"

Wait... I don't get what you mean.
11 and 12 are int type wasn't it? What I want was type number 11, 12 on the keyboard.

pan64 07-08-2013 01:40 AM

as integers 11 and 12 mean the 11th and 12th ascii chars, and they are both control characters, cannot be entered.
as strings "11" and "12" can be entered but cannot be handled by System.in.read() (because it handles only single keypresses/chars)
see here: http://www.abbeyworkshop.com/howto/j...ine/index.html

devnull10 07-08-2013 06:28 AM

And also I would suggest not to use a variable name "type".

fhleung 07-09-2013 06:43 PM

I just don't get what you mean

I made changes to the above code using InputStreamReader and BufferedReader. Please run the above code.

"type could be char, short, int, long or enumerated."
Please show me a similar code example that the INPUT type is int

psionl0 07-10-2013 05:25 AM

Try a simpler interactive language (like BASIC) where you can quickly learn the difference between integers, bytes, characters and strings.

THEN go back to a professional language like Java.

fhleung 07-10-2013 05:47 AM

"type could be char, short, int, long or enumerated."
Please show me a similar code example that the INPUT type is int

pan64 07-10-2013 07:14 AM

you can find some examples here: http://www.roseindia.net/tutorialsea...System.in.read

psionl0 07-10-2013 07:46 AM

Quote:

Originally Posted by fhleung (Post 4987617)
"type could be char, short, int, long or enumerated."
Please show me a similar code example that the INPUT type is int

Quote:

Originally Posted by fhleung (Post 4986004)
What I want was type number 11, 12 on the keyboard.

It sounds like you want to input character strings like "11", "12" etc and then convert them to integers.

I'm not sure that Java's System.in class has methods that do this directly. However, the java.lang.Integer class has methods which will convert the input strings to integers.

Soderlund 07-10-2013 02:22 PM

You want to read integers from the keyboard, right? Any reason not to use the Scanner class?

Code:

Scanner scan = new Scanner(System.in);
while (!scan.hasNextInt())
{
    scan.next(); // Discard non-integer.
}
switch (scan.nextInt())
{
    ...
}



All times are GMT -5. The time now is 08:09 AM.