PDA

View Full Version : Really easy and stupid problem


jenius
04-07-2010, 06:26 PM
So I'm a huge beginner here, but I'm just trying to write something super simple, and it won't compile - not sure what I'm doing wrong. Can anyone toss me some help here?


import javax.swing.*;

public class Testing {
public static void main(String args[]) {

int num = JOptionPane.showInputDialog("Pick a number from 1 to 5");

if (num == 1) {
System.out.println("your a faggot");
} else if (num == 2) {
System.out.println("your the man");
} else if (num == 3) {
System.out.println("your retarded");
} else if (num == 4) {
System.out.println("i love you");
} else if (num == 5) {
System.out.println("you are god");
} else {
System.out.println("You cheated, and entered in" + num + "Try reading the instructions, *****");
}
}
}


Thank you so much everybody!

deXypher
04-08-2010, 03:16 AM
Um... I'm not entirely sure how javax.swing works, as I've never worked with it, but generally for something like this I'd probably use the Scanner utility (found below). When throwing your code into Eclipse, it's telling me that a String cannot be converted into an int for your JOptionPane panel. Possibly you're using that import wrong? I'm not sure.

import java.util.Scanner;

public class Testing {
public static void main(String args[]) {

int num;
Scanner input = new Scanner(System.in);

System.out.println("Pick a number from 1 to 5");
num = input.nextInt();

if (num == 1) {
System.out.println("your a faggot");
} else if (num == 2) {
System.out.println("your the man");
} else if (num == 3) {
System.out.println("your retarded");
} else if (num == 4) {
System.out.println("i love you");
} else if (num == 5) {
System.out.println("you are god");
} else {
System.out.println("You cheated, and entered in" + num + "Try reading the instructions, *****");
}
}
}

Fou-Lu
04-08-2010, 04:58 AM
Scanner has no use in a GUI environment.
Always post your error, in this I presume a datatype mismatch. Input through swing is a String, so you'll need to cast that before storing it in a int . Check the API for the Integer wrapper class, there should be a static method for parseInt I believe its called.

In the future, please clean up the language used in your source code.

Old Pedant
04-08-2010, 07:22 PM
In the future, please clean up the language used in your source code.

It's certainly why I did not reply to him.