Results 1 to 15 of 15
  1. #1
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default [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");
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    andy_man's Avatar
    andy_man is offline Member
    Join Date
    Oct 2008
    Location
    Yerevan
    Posts
    14
    Rep Power
    0

    Default

    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 :)

  5. #5
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    I am really not sure how I would declare and initialize the variables here. I will take a stab at it:

    Java Code:
    static Scanner console = new Scanner(System.in);
    char dem = 'D'
    char rep = 'R'
    String affiliation
    affiliation = console.next();
    Is that correct?

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    yup assuming console.next() exists as a string then everything has been declared and initialized.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    affiliation needs to a char, though, or it won't work in the switch statement.

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  9. #9
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    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");
    }
    }
    }
    }

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    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. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Wink init

    Quote Originally Posted by andy_man View Post
    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).
    Java Code:
    class Initial
    {
       char affiliation = 'I ';//
       main ....
    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.
    Last edited by Nicholas Jordan; 11-19-2008 at 01:38 PM. Reason: code tags
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  12. #12
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default 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

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    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.

  14. #14
    ronda is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  1. Help with switch color
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 09-18-2008, 08:54 AM
  2. How to use Switch keyword
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:07 PM
  3. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 AM
  4. Switch Statemet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-30-2007, 10:16 AM
  5. Adding Variables and using Switch
    By notnumber6 in forum New To Java
    Replies: 4
    Last Post: 11-03-2007, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •