Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
java help
Reply
Posted 2009-05-11, 07:22 AM
ok i have java question i had to do this for class but it doesnt work.
they program ask user for their age and tell them if they can drink, drive or retire here what i have so far tell me what i am doing wrong.

import javax.swing.JOptionPane;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);


//String name;


//name = JOptionPane.showInputDialog("what is your name");
//int names = Integer.parseInt(name);

String age = JOptionPane.showInputDialog("what is your age");
int ages = Integer.parseInt (age);

if (ages < 18){

System.out.println("you are too young to drive");
System.out.println(" you are too young to drink");
System.out.println("you are too young to retire");

}else if (ages >= 21){

System .out.println("you can drive");
System.out.println("you can go to bar and drink");
System.out.println("you can't retire yet");

}else if (ages >= 65){
System.out.println("you can drive");
System.out.println("you can drink");
System.out.println("you can retire now");

}
}
}
Old
Profile PM WWW Search
osmoses-jones is neither ape nor machine; has so far settled for the in-betweenosmoses-jones is neither ape nor machine; has so far settled for the in-between
 
osmoses-jones
 



 
Reply
Posted 2009-05-11, 08:24 AM in reply to osmoses-jones's post "java help"
osmoses-jones said:
if (ages < 18){

System.out.println("you are too young to drive");
What country you from?

Well I am no programmer so my help might be useless. Try structuring it differently...
Code:
x = "drink"
y = "drive"
z = "retire"

if (ages < 18)
System.out.println("You cannot 'x', 'y', or 'z'. ");

}else if (ages >= 21){

System .out.println("You can 'x', 'y', but cannot 'z'. ");

}else if (ages >= 65){
System.out.println("You can 'x' 'y' and 'z'");
All the rest of the technicalities of the programming language I know nothing about, but that is the way I would structure it I guess.
Old
Profile PM WWW Search
Sum Yung Guy seldom sees opportunities until they cease to beSum Yung Guy seldom sees opportunities until they cease to beSum Yung Guy seldom sees opportunities until they cease to beSum Yung Guy seldom sees opportunities until they cease to be
 
 
Sum Yung Guy
 



 
Reply
Posted 2009-05-11, 08:26 AM in reply to osmoses-jones's post "java help"
I can't see anything that would give you a problem if your input is right - that is, if the user enters a number. If they enter a letter or any other character, however, then you'll get a NumberFormatException. What's the error that you keep on getting (and which line?)? Btw, which program are you using to program in?

Have you done anything about exceptions, and using Try and Catch yet, or anything about defensive programming?

EDIT: If you've not done exceptions and try/catch, then I won't bother giving you an answer with them - it'll just serve to confuse you until you learn about them.

I got a happy feeling inside when I saw the code, by the way - that's one of the first things I was taught when I was learning VB.

Oh, and what SYG suggests will make your code look prettier, but not much else (which isn't a bad thing - pretty code is always much easier to read than unpretty code).

Last edited by Lenny; 2009-05-11 at 08:34 AM.
Old
Profile PM WWW Search
Lenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basics
 
 
Lenny
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
JAVA help platnum Tech Help 2 2006-04-10 05:31 AM
Java and C#? pr0xy Tech Help 8 2005-02-08 07:13 AM
Programming Faq Demosthenes Tech Help 4 2004-10-25 04:43 PM
Java help Ganga Tech Help 12 2004-02-02 02:15 PM
Java Script help Dhelds Tech Help 0 2003-01-14 05:07 PM


All times are GMT -6. The time now is 08:05 PM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.