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 8 of 8

Thread: help with code plss

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy help with code plss

    Right, i need to get this code working so that i know how to program this as it's apart of a much larger program.

    I wan't the variable to be String & display a box that asks user to enter an input. The program will read what the user has put such as shortcuts like:

    M, for Monday
    Tu, for Tuesday for example.

    All i want this program to do is to recognise that it's put in M and then come up with a pop up box displaying "Monday" - if the user types M - else it will say Error.

    This is my code so far for it using If statements, but i don't know why it will not compile correctly... Here is my code followed by the compiler error


    import javax.swing.JOptionPane; 
    public class DaysOfWeek 
    { 
    public static void main(String[] args) 
    { 
     
    input1 = JOptionPane.showInputDialog("Enter your input"); 
    { 
    if (input1 == "M") 
     
    JOptionPane.showMessageDialog("Monday"); 
    } 
    else 
    { 
    JOptionPane.showMessageDialog("Error"); 
    } 
     
    } 
    }

    Compiler Error=

    *File location*: 'else' without 'if'
    else
    ^
    1 error
    _____________________________________________

    I don't know why it is erroring saying i do not have an 'if' when blaitantly i have and it doesen't seem to understand that i have an if but more logically i've misplaced something or done something wrong. Please help



    help pls I want to learn why my code is wrong and how to overcome this problem so that i can be a better programmer (im a newbie at the moment)
    Last edited by helloworld922; November 2nd, 2009 at 04:13 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,354
    Thanks
    182
    Thanked 832 Times in 775 Posts
    Blog Entries
    5

    Default Re: help with code plss

    Check your curly bracket placement. Statements need to be balanced by these brackets, so if you move the one above the if to after/below the if it should compile. For example, in pseudo-code

    if (//statement)
    {
    //do something
    }
    else
    {
    //do something else
    }

  3. The Following User Says Thank You to copeg For This Useful Post:

    tonka (November 3rd, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: help with code plss

    I edited it to your recomendation and now i've got these 4 errors which i can't make heads or tails of.
    all i did was move the { bracket away from before the if to after the if. lol now i'm proper confused with it
    =


    C:\Documents and Settings\Tonka\Desktop\Programming\part2-theprogram\New Folder\DaysOfWeek.java:7: cannot find symbol
    symbol : variable input1
    location: class DaysOfWeek
    input1 = JOptionPane.showInputDialog("Enter your input");
    ^
    C:\Documents and Settings\Tonka\Desktop\Programming\part2-theprogram\New Folder\DaysOfWeek.java:9: cannot find symbol
    symbol : variable input1
    location: class DaysOfWeek
    if (input1 == "M")
    ^
    C:\Documents and Settings\Tonka\Desktop\Programming\part2-theprogram\New Folder\DaysOfWeek.java:11: cannot find symbol
    symbol : method showMessageDialog(java.lang.String)
    location: class javax.swing.JOptionPane
    JOptionPane.showMessageDialog("Monday");
    ^
    C:\Documents and Settings\Tonka\Desktop\Programming\part2-theprogram\New Folder\DaysOfWeek.java:15: cannot find symbol
    symbol : method showMessageDialog(java.lang.String)
    location: class javax.swing.JOptionPane
    JOptionPane.showMessageDialog("Error");
    ^
    4 errors

    Tool completed with exit code 1

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,354
    Thanks
    182
    Thanked 832 Times in 775 Posts
    Blog Entries
    5

    Default Re: help with code plss

    Doh, should've spotted those before. Read those errors closely and they tell you exactly what is the problem. Cannot find symbol ...looks like something isn't defined (hint: input1 is a String?). cannot find symbol: method showMessageDialog(java.lang.String)
    hint: Read the API for
    JOptionPane (Java Platform SE 7 b66)
    and
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). You may wish to use the command line (I/O from the Command Line (The Java™ Tutorials > Essential Classes > Basic I/O)) rather than Swing GUI components to retrieve user entered values.
    Another hint: you should use input1.equals("M") rather than input1 == "M" when comparing strings.
    Last edited by copeg; November 2nd, 2009 at 05:34 PM.

  6. The Following User Says Thank You to copeg For This Useful Post:

    tonka (November 3rd, 2009)

  7. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up Re: help with code plss

    Quote Originally Posted by copeg View Post
    Doh, should've spotted those before. Read those errors closely and they tell you exactly what is the problem. Cannot find symbol ...looks like something isn't defined (hint: input1 is a String?). cannot find symbol: method showMessageDialog(java.lang.String)
    hint: Read the API for
    JOptionPane (Java Platform SE 7 b66)
    and
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). You may wish to use the command line (I/O from the Command Line (The Java™ Tutorials > Essential Classes > Basic I/O)) rather than Swing GUI components to retrieve user entered values.
    Another hint: you should use input1.equals("M") rather than input1 == "M" when comparing strings.
    Ahhhh i see!

    Why is it when i'm at a dead end I feel like i can never code this and get it to work.. then something silly comes up and it works and i feel stupid after LOL

    thanks m8
    mostly appretiated

    i've given you some rep & a thanks for your help

  8. #6
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: help with code plss

    I understand the code edits i have made but i'm not fully aware of still whats wrong with my code. i'm just reading through the information on java.sun website you gave to me in order to understand

    here is my code as it stands:-


    import javax.swing.JOptionPane;
    public class test
    {
    	public static void main (String[] args)
    	{
    	String input1 = JOptionPane.showInputDialog("Enter your input");
     
    	if (input1.equals ("M")) // it errors me if i do not put these (()) where they are
    	{
    		JOptionPane.showMessageDialog("Monday");
    	}
    	else
    	{
    		JOptionPane.showInputDialog("Invalid Input");
    }
     
    }
    }

    Errors:-


    C:\Documents and Settings\Tonka\Desktop\Programming\part2-theprogram\test.java:10: cannot find symbol
    symbol : method showMessageDialog(java.lang.String)
    location: class javax.swing.JOptionPane
    JOptionPane.showMessageDialog("Monday");
    ^
    1 error

    Tool completed with exit code 1
    Last edited by helloworld922; November 3rd, 2009 at 10:29 AM.

  9. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: help with code plss

    What you need in JOptionPane.showMessageDialog("Monday");
    is "null" in front of Monday.

    It should read: JOptionPane.showMessageDialog(null, "Monday");

    Also, I've spotted another error, after your else statement, your trying to display something, right? How you have it, it's asking for an input.

    Use .showMessageDialog(null, "Invalid Input");
    instead of .showInputDialog

    Those changes should fix your code.

  10. The Following User Says Thank You to prodigytoast For This Useful Post:

    tonka (November 5th, 2009)

  11. #8
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up Re: help with code plss

    Quote Originally Posted by prodigytoast View Post
    What you need in JOptionPane.showMessageDialog("Monday");
    is "null" in front of Monday.

    It should read: JOptionPane.showMessageDialog(null, "Monday");

    Also, I've spotted another error, after your else statement, your trying to display something, right? How you have it, it's asking for an input.

    Use .showMessageDialog(null, "Invalid Input");
    instead of .showInputDialog

    Those changes should fix your code.
    Thank you m8, it has. Working all fine now

    such a silly mistake lool

    thanks m8 much appretiated