Results 1 to 15 of 15
- 11-23-2011, 09:36 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
how many ways can I use a switch statement
I'm trying to come up with multiple ways to use a switch statement instead of the one way that was shown in the TUT
I'm wondering if I can have the end user punch in a number and it goes exactly to the case the number that coincides with the specific case
EX:
I want it to be possible for the user to punch in which case they wantJava Code:import java.util.Scanner; public class SwitchCase { public static void main(String arg[]){ int Account1; int Account2; int Account3; Account1 = 1441; Account2 = 2441; Account3 = 3441; Scanner Info=new Scanner(System.in); System.out.println(Info.nextLine()); switch (Account1){ case 1441: System.out.println("Your Name is Chuck Thomas" ); break; case 2441: System.out.println("Your Name is James Waverly"); break; case 3441: System.out.println("Your name is Johnny Knoxville"); default: System.out.println("Account not present"); break;Last edited by Norm; 11-23-2011 at 09:55 PM. Reason: fixed ending code tag
- 11-23-2011, 09:54 PM #2
Re: how many ways can I use a switch statement
What do you want the user to enter: The full number(4 digits) used in the switch or do you want it simpler like: 1,2, 3
You can have more than one case: statement for a block of code.
Ask the user the question, get his response as an int (or String and convert to int) and use that in the switch
- 11-23-2011, 10:17 PM #3
Re: how many ways can I use a switch statement
In Java 7, you can also use Strings in a switch. The usual condition of cases being compile time constants still applies.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-23-2011, 10:19 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
Re: how many ways can I use a switch statement
So I would write the cin>> like this
Java Code:import java.util.Scanner class name{ public static void main(String arg[]){ System.out.out.println("enter your four digit number); Scanner input = new Scanner (System.in); int Number; Number = (input); switch (Number){ case 1:Last edited by jaytee; 11-23-2011 at 10:25 PM.
- 11-23-2011, 10:21 PM #5
Re: how many ways can I use a switch statement
Does your code compile ok?
Before posting, you'd save some time if you'd compile it and get rid of the compiler errors before posting.
Also if you don't understand what the error message says, you could post the full text of the error message with your question.
- 11-23-2011, 10:28 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
Re: how many ways can I use a switch statement
it compiles but when I enter any of the 4 digit numbers it gives me the first case only
(e.g. 2441
2441
Your Name is Chuck Thomas)
only 1441 is supposed to be Chuck Thomas
- 11-23-2011, 10:30 PM #7
Re: how many ways can I use a switch statement
Please post the code that you are currently executing.
- 11-23-2011, 10:35 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
Re: how many ways can I use a switch statement
This is the code I'm currently posting and it compiles, it just doesn't do exactly what I want it to do
Java Code:import java.util.Scanner; public class SwitchCase { public static void main(String arg[]){ int Account1; int Account2; int Account3; Account1 = 1441; Account2 = 2441; Account3 = 3441; Scanner Info=new Scanner(System.in); System.out.println(Info.nextLine()); switch (Account1){ case 1441: System.out.println("Your Name is Chuck Thomas" ); break; case 2441: System.out.println("Your Name is James Waverly"); break; case 3441: System.out.println("Your name is Johnny Knoxville"); default: System.out.println("Account not present"); break;
- 11-23-2011, 10:37 PM #9
Re: how many ways can I use a switch statement
That code will not compile. For starters it is missing 3 ending }s
What is the value of Account1 when you execute the switch statement?
Add a println just before the switch statement to print out the value of Account1 so you can see.Last edited by Norm; 11-23-2011 at 10:40 PM.
- 11-23-2011, 11:19 PM #10
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
Re: how many ways can I use a switch statement
I hope I followed your directions right but this is what I put before the switch statement
after it compiled the screen was blank until I punched in Account1Java Code:System.out.println(Account1); switch (Account1){
and this is what popped up
Java Code:Account1 Account1 1441 Your Name is Chuck Thomas
- 11-23-2011, 11:23 PM #11
Re: how many ways can I use a switch statement
Is that the correct way for the switch statement to work when Account1 has the value: 1441?
You need to read a value in from the user and store it in Account1 to give the user control over what happens.
- 11-23-2011, 11:30 PM #12
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
Re: how many ways can I use a switch statement
so
is not the way to read the value in?Java Code:import java.util.Scanner; and Scanner Info=new Scanner(System.in);
what is the correct syntax for that code?
- 11-23-2011, 11:38 PM #13
Re: how many ways can I use a switch statement
After you define a Scanner object, you must use one of the Scanner class's next... methods to read data into a variable.
Look at the API doc for the Scanner class to see which method to use.
Also there are lots of code samples on the forum. Search here for Scanner or use Google
- 11-23-2011, 11:42 PM #14
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
Re: how many ways can I use a switch statement
where can I find the API docs?
- 11-23-2011, 11:49 PM #15
Re: how many ways can I use a switch statement
This site has doc for the SE classes:
Java Platform SE 6
Find the class in the lower left, click on it and the doc is displayed in the main window.
Similar Threads
-
switch statement
By droidus in forum New To JavaReplies: 2Last Post: 09-21-2011, 09:54 AM -
Switch Case statement
By seanfmglobal in forum New To JavaReplies: 7Last Post: 02-15-2011, 01:18 PM -
help with switch statement
By java__beginner in forum New To JavaReplies: 4Last Post: 03-19-2009, 02:22 PM -
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02-06-2008, 05:16 AM -
Help with gigantamous switch statement
By trill in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks