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: 2nd week with Java. Help with a few basic syntax errors?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 2nd week with Java. Help with a few basic syntax errors?

    //I am the definition of a programming newbie. When I try to compile this code, I get a few syntax errors towards the end- mostly "identifier expected." I realize this is about as basic as debugging gets, but I can't figure this out.
     
     
    import javax.swing.*;
     
    public class SimpleGUIApplications {
     
      public static void main(String[] s);
     
        String name;
     
    		String promptForName = "Enter your name: ";
     
    		String promptForCredits = ", enter number of credits you have completed";
     
        String creditsString;
     
    		int credits;
     
    			String feedbackPart1 = "You need credits to ";
     
    			String feedbackPart2 = " graduate.";
     
    		String name = JOptionPane.showInputDialog(promptForName);
     
    		String creditsString = JOptionPane.showInputDialog(name + promptForCredits);
     
    		String credits = Integer.parseInt(creditsString);
     
    	JOptionPane.showMessageDialog(null, name + ", " + feedbackPart1 +
     
        credits + feedbackPart2);
     
    }
    Last edited by D P; January 28th, 2012 at 02:50 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: 2nd week with Java. Help with a few basic syntax errors?

    Please use highlight tags when posting code. This is how to do that:

    [highlight=Java]//Your code here[/highlight]

    Give me a moment to look over your code...
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: 2nd week with Java. Help with a few basic syntax errors?

    String name = JOptionPane.showInputDialog(promptForName);

    String creditsString = JOptionPane.showInputDialog(name + promptForCredits);

    String credits = Integer.parseInt(creditsString);
    Here're your issues:

    You have already defined Strings name and creditsString, so you do not need to define them again.

    Next, you are trying to define the variable credits as a String when you already have a variable named credits defined as an integer earlier in your program.

    To assign a value to a variable, you do not need to declare it again. This is the syntax:

    variable = value;

    So, for example (assume all variables are ints):

    number = 7;
    otherNumber = 3 + 2;
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. The Following User Says Thank You to snowguy13 For This Useful Post:

    D P (January 28th, 2012)

  5. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 2nd week with Java. Help with a few basic syntax errors?

    Alright, so when I pulled "String" out of those three lines I am getting even more syntax errors now. Specifically "identifier expected" after name, creditsString, and credits. I understand you probably feel like you're typing to a 3rd grader right now, but your help is appreciated.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 2nd week with Java. Help with a few basic syntax errors?

    Could you post the error messages you're getting?

  7. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 2nd week with Java. Help with a few basic syntax errors?

    SimpleGUIApplications.java:21: error: <identifier> expected
                    name = JOptionPane.showInputDialog(promptForName);
                        ^
    SimpleGUIApplications.java:23: error: <identifier> expected
                    creditsString = JOptionPane.showInputDialog(name + promptForCred
    its);
                                 ^
    SimpleGUIApplications.java:25: error: <identifier> expected
                    credits = Integer.parseInt(creditsString);
                           ^
    SimpleGUIApplications.java:27: error: <identifier> expected
            JOptionPane.showMessageDialog(null, name + ", " + feedbackPart1 +
                                         ^
    SimpleGUIApplications.java:27: error: illegal start of type
            JOptionPane.showMessageDialog(null, name + ", " + feedbackPart1 +
                                          ^
    SimpleGUIApplications.java:27: error: <identifier> expected
            JOptionPane.showMessageDialog(null, name + ", " + feedbackPart1 +
                                                    ^
    SimpleGUIApplications.java:27: error: ';' expected
            JOptionPane.showMessageDialog(null, name + ", " + feedbackPart1 +
                                                      ^
    SimpleGUIApplications.java:27: error: illegal start of type
            JOptionPane.showMessageDialog(null, name + ", " + feedbackPart1 +
                                                            ^
    SimpleGUIApplications.java:27: error: ';' expected
            JOptionPane.showMessageDialog(null, name + ", " + feedbackPart1 +
                                                                           ^
    SimpleGUIApplications.java:29: error: <identifier> expected
        credits + feedbackPart2);
               ^
    SimpleGUIApplications.java:29: error: <identifier> expected
        credits + feedbackPart2);
                               ^
    11 errors
    Last edited by D P; January 28th, 2012 at 03:23 PM.

  8. #7
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: 2nd week with Java. Help with a few basic syntax errors?

    Hi,

    public static void main(String[] s);

    Since this is your method, you're going to need curly braces afterwards to encase the rest of your code from within that method.

    public static void main(String[] s){
       ...
    }

  9. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: 2nd week with Java. Help with a few basic syntax errors?

    when you are creating classes except abstracts, you should always keep this in mind, think of methods as structures like, if - else, switch(selection structures), for-loop, do-while, while(repetition structures), java class(class structure) where you always need to put a matching braces and never semi-colons, semi-colons are used for statements/compound statements(remember im talking about classes), think of semi-colons as comma or period when you are writing sentences in an essay or paragraph, period is used to indicate that your thought in a sentence is done, same as statements in java, semi-colons are used to indicate that the thing that you need in your statement is done means that you are writing another statement(sentence)


    I want to tell you I am so 9gag. <-- sentence/phrase, end with period
    I am amazed with a troll face. <-- sentence/phrase, end with period
    System.out.println("I want to tell you im so 9gag"); // statement, end with semi-colon
    System.out.println("I am amazed with a troll face"); // statement, end with semi-colon

    i always keep this in mind, at my first beginning of java programming, to avoid confusion on where and when will i use semi-colons and braces
    Last edited by chronoz13; January 28th, 2012 at 11:18 PM.

Similar Threads

  1. Willing to volunteer 10 hours per week of programming time
    By Otiose Dodge in forum Paid Java Projects
    Replies: 6
    Last Post: August 27th, 2012, 02:09 PM
  2. Java Equation correct syntax
    By macko in forum What's Wrong With My Code?
    Replies: 18
    Last Post: November 5th, 2011, 12:16 AM
  3. How to print the day of the week from user input?
    By f0x in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2011, 06:33 PM
  4. java syntax highlighting
    By Saulius in forum Java Theory & Questions
    Replies: 2
    Last Post: August 24th, 2011, 06:47 AM
  5. hi - Java swing errors
    By genlastudio in forum AWT / Java Swing
    Replies: 1
    Last Post: March 22nd, 2011, 06:49 PM