Java Troubles

Sports, politics, movies, videogames, questionable hobbies, photos from your family vacation, etc. Talk about stuff that isn't ponies or music. But do try to stay on topic and respectful of alternate opinions.

Java Troubles

Postby senntenial » 03 Sep 2012 00:59

Now this has just about 0 stuff to do with ponies, but being community where you can post most anything, knowing many of you guys code, and knowing how fast responses are and my desire not to have to sign up for a different forum, here ya go.

It's just a basic code that prompts the user with a question (what is the password).
When the user responds with the correct answer, a dialogue should appear saying so. I have gotten to be able to enter text, but as soon as I press "OK" the application ends. I'm sure this is a noobie issue, but if someone could explain it to me I'd be much obliged.

This method has worked with just basic console, but once I try adding the JOptionPane things stop working.

Code: Select all
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TutorialProject {
   
   static Scanner input = new Scanner(System.in);
   public static void main(String[] args){
      inputTest();
   
   }
   public static void inputTest(){
      JOptionPane.showInputDialog("Enter the password: ");
      String message = input.nextLine();
      
      if (message.equals("password"))
         JOptionPane.showMessageDialog(null, "Good job, that's the password.");
}
}
}
}
My website with free music resources
18 year old full stack web dev and designer
User avatar
senntenial
Global Moderator
 
Posts: 634
Joined: 25 Feb 2012 01:44
Location: Pennsylvania
OS: Windows 7-8, Ubuntu
Primary: FL Studio
Cutie Mark: ponies are for losers

Re: Java Troubles

Postby NewWorldMare » 03 Sep 2012 05:33

The only thing wrong here is that you're using a Scanner to get the input. JOptionPane returns the user's input, so all you have to do is make message equal to what JOptionPane returns

(There are also two too many braces at the end. :) )

Code: Select all
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TutorialProject {
   
   static Scanner input = new Scanner(System.in);
   public static void main(String[] args){
      inputTest();
   
   }
   public static void inputTest(){
 
      String message = JOptionPane.showInputDialog("Enter the password: ");
     
      if (message.equals("password"))
         JOptionPane.showMessageDialog(null, "Good job, that's the password.");
}
}
Pianist - Producer - FL Studio 10 - Youtube - Soundcloud - Tumblr - Facebook
User avatar
NewWorldMare
 
Posts: 67
Joined: 03 Jun 2012 16:50

Re: Java Troubles

Postby senntenial » 03 Sep 2012 10:08

Awesome! Fixed it right up. Thank you.
My website with free music resources
18 year old full stack web dev and designer
User avatar
senntenial
Global Moderator
 
Posts: 634
Joined: 25 Feb 2012 01:44
Location: Pennsylvania
OS: Windows 7-8, Ubuntu
Primary: FL Studio
Cutie Mark: ponies are for losers


Return to Off-Topic Discussion



Who is online

Users browsing this forum: No registered users and 9 guests