LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2013, 11:53 PM   #1
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Rep: Reputation: 30
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.

Last edited by fhleung; 07-06-2013 at 08:48 PM.
 
Old 07-06-2013, 01:18 AM   #2
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.

Last edited by psionl0; 07-06-2013 at 01:36 AM.
 
Old 07-06-2013, 08:57 PM   #3
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
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();
 
Old 07-06-2013, 09:24 PM   #4
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by fhleung View Post
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.
 
Old 07-06-2013, 09:42 PM   #5
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
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?

Last edited by fhleung; 07-10-2013 at 05:38 AM.
 
Old 07-06-2013, 11:51 PM   #6
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.
 
Old 07-08-2013, 01:25 AM   #7
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
"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.
 
Old 07-08-2013, 01:40 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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
 
Old 07-08-2013, 06:28 AM   #9
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
And also I would suggest not to use a variable name "type".
 
Old 07-09-2013, 06:43 PM   #10
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
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

Last edited by fhleung; 07-10-2013 at 05:37 AM.
 
Old 07-10-2013, 05:25 AM   #11
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.
 
Old 07-10-2013, 05:47 AM   #12
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
"type could be char, short, int, long or enumerated."
Please show me a similar code example that the INPUT type is int
 
Old 07-10-2013, 07:14 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you can find some examples here: http://www.roseindia.net/tutorialsea...System.in.read
 
Old 07-10-2013, 07:46 AM   #14
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by fhleung View Post
"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 View Post
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.
 
Old 07-10-2013, 02:22 PM   #15
Soderlund
Member
 
Registered: Aug 2012
Posts: 185

Rep: Reputation: 81
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())
{
    ...
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem breaking out of nested switch java. xskycamefalling Programming 2 09-21-2010 03:26 AM
Not able to kill java process with average 90% cpu usage Squall90 Slackware 13 05-12-2010 03:02 PM
How to switch java versions... markw8500 Programming 3 09-24-2006 06:54 PM
Memory usage problem - Java MRMadhav Programming 16 07-20-2006 09:27 AM
monitor java thread usage against o/s limits rlangsto Linux - General 1 02-11-2005 12:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration