switch usage in Java, programming rule/grammar
switch logic is available almost all types of programming language.
Code:
int type; 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. |
Do you mean something like this?
Code:
switch(type) { |
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(); |
Quote:
The standard Java console is not designed for inputting individual characters. |
Code:
import java.util.*; Enter pressED after something on keyboard keyin Please run the above code. It always output default. Any idea? |
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. |
"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. |
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 |
And also I would suggest not to use a variable name "type".
|
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 |
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. |
"type could be char, short, int, long or enumerated."
Please show me a similar code example that the INPUT type is int |
you can find some examples here: http://www.roseindia.net/tutorialsea...System.in.read
|
Quote:
Quote:
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. |
You want to read integers from the keyboard, right? Any reason not to use the Scanner class?
Code:
Scanner scan = new Scanner(System.in); |
All times are GMT -5. The time now is 08:09 AM. |