Results 1 to 4 of 4
Thread: First attempt using swing
- 09-17-2011, 06:05 PM #1
Member
- Join Date
- Sep 2011
- Location
- Bristol, England
- Posts
- 5
- Rep Power
- 0
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);
}
}
- 09-17-2011, 06:18 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
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); } }
- 09-17-2011, 06:25 PM #3
Member
- Join Date
- Sep 2011
- Location
- Bristol, England
- Posts
- 5
- Rep Power
- 0
Re: First attempt using swing
Thank you Fabken. I realised the spelling mistakes after posting it.
I never realised that the StrAboutMe had to begin with a capital. Thank you very much for your help!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.";
- 09-17-2011, 07:33 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: First attempt using swing
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
First attempt at Java.
By mxr808 in forum New To JavaReplies: 6Last Post: 11-27-2010, 09:04 PM -
First TicTacToe Attempt
By Pyrexkidd in forum New To JavaReplies: 2Last Post: 07-25-2010, 09:32 PM -
Switching JPanels inside JFrame attempt
By frenk_castle in forum AWT / SwingReplies: 7Last Post: 03-31-2010, 08:39 AM -
my Quicksort attempt has failed
By Jeremy8 in forum New To JavaReplies: 4Last Post: 11-16-2009, 02:56 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks