Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fabken

Thread: First attempt using swing

  1. #1
    B3rtbot is offline Member
    Join Date
    Sep 2011
    Location
    Bristol, England
    Posts
    5
    Rep Power
    0

    Default First attempt using swing

    I'm sorry if this is in the wrong category. I've created this small AboutMe program which asks the user to pick an option for something they want to know. However, it doesn't compile because of a piece of code is wrong. Any help would be appreciated.

    import javax.swing.JOptionPane;

    public class AboutMe {

    public static void main(String[] args) {

    String strName, strResponse, StrAboutMe =" "; int intChoise = 0;
    strName = JOptionPane.showInputDialog("Your name:");
    strResponse = JOptionPane.showInputDialog("Hello " + strName + ", please choose what you would like to know about me:\n" +
    "1 My name\n" +
    "2 My age\n" +
    "3 The school I attend\n" +
    "4 My hobbies\n" +
    "5 Something else about me\n");
    int Choice = Integer.parseInt(strResponse);

    switch(int Choice) {
    case 1 : strAboutMe = ("My name is Barclay"); break;
    case 2 : strAboutMe = "I am sixteen"; break;
    case 3 : strAboutMe = "My school is The Grange School and Sports College"; break;
    case 4 : strAboutMe = "I enjoy programming and listening to music"; break;
    case 5 : strAboutMe = "I like maths." + "\n\t" + "Thank you for asking.";
    }
    if (int Choice>0 && int Choice<6) JOptionPane.showMessageDialog(null, strAboutMe);
    }

    }

  2. #2
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Re: First attempt using swing

    Hello,

    You made a lot of spelling mistakes and I corrected everything for you.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Test {
    
    public static void main(String[] args) {
    
    String strName, strResponse, StrAboutMe =" ";
    int intChoise = 0;
    strName = JOptionPane.showInputDialog("Your name:");
    strResponse = JOptionPane.showInputDialog("Hello " + strName + ", please choose what you would like to know about me:\n" +
    "1 My name\n" +
    "2 My age\n" +
    "3 The school I attend\n" +
    "4 My hobbies\n" +
    "5 Something else about me\n");
    int Choise = Integer.parseInt(strResponse);
    
    switch(Choise) {
    case 1 : StrAboutMe = ("My name is Barclay"); break;
    case 2 : StrAboutMe = "I am sixteen"; break;
    case 3 : StrAboutMe = "My school is The Grange School and Sports College"; break;
    case 4 : StrAboutMe = "I enjoy programming and listening to music"; break;
    case 5 : StrAboutMe = "I like maths." + "\n\t" + "Thank you for asking.";
    }
    if (Choise>0 && Choise<6) JOptionPane.showMessageDialog(null, StrAboutMe);
    }
    
    }
    B3rtbot likes this.

  3. #3
    B3rtbot is offline Member
    Join Date
    Sep 2011
    Location
    Bristol, England
    Posts
    5
    Rep Power
    0

    Default Re: First attempt using swing

    Thank you Fabken. I realised the spelling mistakes after posting it.

    Java Code:
    switch(Choise) {
    case 1 : StrAboutMe = ("My name is Barclay"); break;
    case 2 : StrAboutMe = "I am sixteen"; break;
    case 3 : StrAboutMe = "My school is The Grange School and Sports College"; break;
    case 4 : StrAboutMe = "I enjoy programming and listening to music"; break;
    case 5 : StrAboutMe = "I like maths." + "\n\t" + "Thank you for asking.";
    I never realised that the StrAboutMe had to begin with a capital. Thank you very much for your help!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default Re: First attempt using swing

    Quote Originally Posted by B3rtbot View Post
    I never realised that the StrAboutMe had to begin with a capital. Thank you very much for your help!
    It doesn't need to start with a capital letter; as a matter of fact it is against the coding style convention to make a variable start with a capital letter. The convention is 'strAboutMe'. b.t.w. the spelling is 'choice', not 'choise' (both with a lower case c).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. First attempt at Java.
    By mxr808 in forum New To Java
    Replies: 6
    Last Post: 11-27-2010, 09:04 PM
  2. First TicTacToe Attempt
    By Pyrexkidd in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 09:32 PM
  3. Switching JPanels inside JFrame attempt
    By frenk_castle in forum AWT / Swing
    Replies: 7
    Last Post: 03-31-2010, 08:39 AM
  4. my Quicksort attempt has failed
    By Jeremy8 in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 02:56 AM

Tags for this Thread

Posting Permissions

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