Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   java help (http://zelaron.com/forum/showthread.php?t=48912)

osmoses-jones 2009-05-11 07:22 AM

java help
 
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");

}
}
}

Sum Yung Guy 2009-05-11 08:24 AM

Quote:

Originally Posted by osmoses-jones
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.

Lenny 2009-05-11 08:26 AM

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).


All times are GMT -6. The time now is 11:36 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.