Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: if statement help

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default if statement help

    public class christmas {
    	public static char songNumber (int songResult){
    		char song;
    	if (songResult = 1){
    		song = 'A Partridge in a Pear Tree';   **this is the line with the error, it says invalid character constant where the 'A is
    	}else (songResult = 2){
    			song = '2 Turtle Doves and a Partridge in a Pear Tree';
    		}
    			return song;
    	}
     
    	public static void main(String[] args) {
    	christmas rc = new christmas ();
     
    	char yourSong = songNumber(1);
    	System.out.println("On the" + yourSong);
    	}
    }
    I have highlighted the error in the code at about the sixth line
    Why have I got an error? I need help quick!

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: if statement help

    Char variables only contain a single character, and they are defined like 'a', 'b', or 'c'.

    Strings can contain any number of characters and are defined in "double quotes", not 'single quotes'.

    Also, your if statement is assigning 1 to your songResult variable, not checking whether your songResult variable is 1. Use == instead.

    Also also, you should really use standard naming conventions and correct indentation.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: if statement help

    Instant fix, thank you!

    Further question though, if I was to carry on with the code with the song, say the outcome is 4 which will add a next line, is it possible to display 4 and 3 together instead of writing loads of text? I know I haven't explained that well!

    Thanks again.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: if statement help

    I'm not really sure what you're asking. Are you just asking about the || operator? Recommended reading: Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: if statement help

    Still slightly confused.

    What I was is say if the result was say 2, I want the number below 2 to be shown as well, so I'd want the 2 and 1 results to both be displayed

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: if statement help

    I suggest writing out the rules that you want to determine what gets printed out. If you can't explain it in English (in a list of rules, not vague descriptions or examples), then you won't be able to write it in code.

    Hint: You can have two if statements, one after the other, without using the else keyword. Something like:

    if(x > 0){
       System.out.println("X is greater than zero!");
    }
    if(x > 10){
       System.out.println("X is greater than ten!");
    }

    If x is, for example, 15, then both print statements will be triggered. If x is 5, only the first one will be triggered.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  4. I need more if-statement help
    By DarkPrince in forum Loops & Control Statements
    Replies: 1
    Last Post: November 7th, 2012, 10:41 PM
  5. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM