Results 1 to 15 of 15
- 11-18-2008, 09:30 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
[SOLVED] Should I use switch or if else
I am trying to write a short program that will output Democrat if the party affiliation code is 'D', Republican if the party code is 'R' and Independent if otherwise. I tried to use a switch structure but when I compile the program I get an error that says "symbol : variable affiliation
location: class hwq2
switch (affiliation)".
^
I am not sure how to declare and initialize this properly or if I should even be trying to do this as a switch structure to begin with. I would appreciate it if someone could look at my code and help me.
Here is my code:
Java Code:switch (affiliation) { case 'D': System.out.println("Democrat"); break; case 'R': System.out.println("Republican"); break; default: System.out.println("Independent"); }
- 11-18-2008, 09:51 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I'm sorry, but how would changing the type of conditional statement used change the fact that the variable "affiliation" does not exist in that scope?
- 11-18-2008, 09:52 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Is that affiliation declared as a char variable?
- 11-18-2008, 01:25 PM #4
You should declare your affiliation variable as char, and initialize it somehow (or read from the console or maybe generate it's value with some difficult calculation).
TEAM = Together Everyone Achieves More :)
- 11-18-2008, 04:45 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
I am really not sure how I would declare and initialize the variables here. I will take a stab at it:
Is that correct?Java Code:static Scanner console = new Scanner(System.in); char dem = 'D' char rep = 'R' String affiliation affiliation = console.next();
- 11-18-2008, 04:52 PM #6
yup assuming console.next() exists as a string then everything has been declared and initialized.
- 11-18-2008, 05:44 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
affiliation needs to a char, though, or it won't work in the switch statement.
- 11-18-2008, 05:52 PM #8
console.next() returns a string correct? If you read in String "D" or "Dem"(not sure how your file is set up) change affiliation to a char and do affiliation = console.next().charAt(0);
That will set affiliation = to D and you can use it in your switch.
- 11-18-2008, 06:25 PM #9
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
Yep, that is how I did it and it compiles and runs like it should. I am psyched. I am very new to Java and I like it. I'd like to thank you all very much for your help. I'll paste my final code below and if there is anything I should have done differently or that is wrong please let me know.
Java Code:import java.io.*; import java.util.*; public class Hwq4 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { char affiliation; System.out.print("Enter the letter of the party affiliation:" + "D for Democrat, R for Republican, and " + "I for Independent \n"); affiliation = console.next().charAt(0); { switch (affiliation) { case 'D': System.out.println("Democrat"); break; case 'R': System.out.println("Republican"); break; default: System.out.println("Independent"); } } } }
- 11-19-2008, 12:25 AM #10
To answer your initial question, in many cases, it makes no difference.
a 'switch' statement is just syntactic sugar on a string of nested if/else if/ elseif/ else statements.
There are more restrictions on what a switch statement can accept in its "case" labels, but in those cases, you have to use the if/else/...
There were languages that had performance differences, but most of them special cased things, so that the compiler would generate a calculated result into a dispatch table. But java doesn't do that, so it makes no difference.
- 11-19-2008, 12:37 PM #11
init
But it is as masijade says. I did not know that Java does not construct a jump table so be that the case what we can do here is refine the pursuit to be what about respondent provides no answer. There we can replace "I" Independent with "U" for undecided or as the logic works to the service of your understanding of what is going on here.Java Code:class Initial { char affiliation = 'I ';// main ....Last edited by Nicholas Jordan; 11-19-2008 at 12:38 PM. Reason: code tags
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 01-18-2009, 03:57 AM #12
Member
- Join Date
- Jan 2009
- Posts
- 1
- Rep Power
- 0
Java help
Hello everyone I am new to the forum and look forward to talking with everyone. I wanted to see if anyone could point me in the correct direction with some Homework i am doing.
int j=o;
the output of the statement
if ((8>4) || (j++==7))
system.out.print1n"j="+j);
is:
j=1
basically i have to explain how one gets to the conclusion
- 01-18-2009, 04:21 AM #13
You should not hijack someone else's thread. What you have posted has zero to do with the original question.
And you should run the code, homework is to help you learn.
- 12-25-2011, 01:17 AM #14
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Re: [SOLVED] Should I use switch or if else
hi please i wont answer from this question
1- Write java statemants that output Democrat if the party affiliation code is 'D' Republican if the party affiliation code 'R' and other otherwise.
2- Correct the following code so that it prints the correct message :
if (score >= 60)
System.out.println (" You pass . ");
else ;
System.out.println (" You fail . ");
please Quickly
- 12-25-2011, 01:49 AM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: [SOLVED] Should I use switch or if else
@ronda: you are hijacking this thread. Start your own thread for your own question.
Also please realise that people here will not write your code for you. If you have a problem explain as fully as you can what you are trying to do and the code you have written. That way you can get help about compiler errors or the program not doing what you want. Or you might ask about some concept you have read about: saying what it is you have read and what it is that is unclear.
But just saying "Do X for me" is not a question and will not recieve a positive response.
-----
Locking the thread
Similar Threads
-
Help with switch color
By Daniel in forum AWT / SwingReplies: 2Last Post: 09-18-2008, 07:54 AM -
How to use Switch keyword
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:07 PM -
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02-06-2008, 05:16 AM -
Switch Statemet
By Java Tip in forum Java TipReplies: 0Last Post: 11-30-2007, 09:16 AM -
Adding Variables and using Switch
By notnumber6 in forum New To JavaReplies: 4Last Post: 11-03-2007, 05:45 PM


LinkBack URL
About LinkBacks


Bookmarks