Skip to main content

First Program Help

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
Birdgeek3
Offline
Joined: 2011-08-13

Hello, Im am very new to Java and only 15 so sorry if i did something wrong. Im using Eclipse if that helps. I am trying to write a script that prompts users to enter their name and then reply with something unique for each person. I get the window to open prompting for the name but then it goes to the default "sorry i don't know you :p" Cna you help me? Here is my code so far: import javax.swing.JOptionPane;  import java.io.*;  class test {  private static double mike;  private static double brandon;  private static double jon;  private static double kala;  private static double robyn;  private static double jean;  public static void main(String[] args) throws IOException {  String userInput = JOptionPane.showInputDialog("Enter your name with no caps");  System.in.read();  double input1 = Double.parseDouble(userInput);  {             if (input1 == mike) {     System.out.println("Hey dude ready to bot?");     } else if (input1 == brandon) {     System.out.println("Hey Parkour?");     } else if (input1 == jon) {     System.out.println("Hey. You like fishing");     } else if (input1== kala) {     System.out.println("Hey mommy!");     } else if (input1 == robyn) {     System.out.println("Hey sis. Fruit ninja later?");     } else if (input1 == jean) {     System.out.println("Hey how's America so far?");     } else      System.out.println("sorry i don't know you :p");     }           }  } 

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
LST
Offline
Joined: 2011-08-21

The main problem is that you've saved your literals into a Double data type. Double contains numeric values. You're asking the user for a string, like "brandon". When this will happen, the program will try to convert this to a numeric format, fail, and throw an Exception.

What you have to do is to declare your variables as String data types, e.g. : String brandon = new String("brandon"); (you have to assign values to variables as well, remember?) And when testing for equality, try something like input1.equals(brandon).