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);
}
}
Re: First attempt using swing
Hello,
You made a lot of spelling mistakes and I corrected everything for you.
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);
}
}
Re: First attempt using swing
Thank you Fabken. I realised the spelling mistakes after posting it.
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!
Re: First attempt using swing
Quote:
Originally Posted by
B3rtbot
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