Results 1 to 3 of 3
  1. #1
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default Problem with a password

    I'm having a problem with a password. The password is an integer, but my problem is that when someone tries to enter a string input, the program crashes, instead i want it to display invalid like when someone tries to enter an invalid integer password. I also wish to display the password in asterik form. What do i have to do? Can someone pls help me? Thanks a lot! :) The code i've written is below:-

    Java Code:
    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    import java.io.*;
     
    import java.util.Scanner;
    import java.util.Arrays;
     
    public class GeographyQuiz extends JFrame implements ActionListener {
     
      private static final int FRAME_WIDTH = 140;
      private static final int FRAME_HEIGHT = 160;
      private static final int FRAME_X_ORIGIN = 70;
      private static final int FRAME_Y_ORIGIN = 50;
     
       AnswerStore answerStore = new AnswerStore();
      
      public static void main (String[] args) {
     
        JFrame jFrame;
        jFrame = new JFrame();
     
        JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
        JOptionPane.showMessageDialog(null, "Good Luck");
     
        char choice;
        int i, choice1, Password;
        String yourChoice, passString;
     
        passString = JOptionPane.showInputDialog("Enter the Password");
     
        //Password = passString.nextInt();
        Password = Integer.parseInt(passString.trim());
     
        if (Password == 123) {
         JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");
     
         GeographyQuiz frame = new GeographyQuiz();
          frame.setVisible(true);
     
        } else {
          JOptionPane.showMessageDialog(null, "Invalid Password. Try Again");
        }
      }
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    First, to hide the password you can use the JPasswordField swing component.

    Second, to prevent your program from crashing you need to catch the NumberFormatException when you parse the input string to interger value, because the parseInt() method of the Integer class throws this exception if the input is not a valid number.
    Last edited by wsaryada; 12-27-2007 at 12:06 PM.

  3. #3
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

Similar Threads

  1. Password System help
    By quickfingers in forum New To Java
    Replies: 1
    Last Post: 06-23-2008, 07:18 PM
  2. Asking for password from a Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-27-2008, 09:05 PM
  3. add password to folder
    By ismailsaleh in forum AWT / Swing
    Replies: 1
    Last Post: 01-08-2008, 06:46 AM
  4. How would I transmit my password through java
    By silvia in forum Networking
    Replies: 1
    Last Post: 07-30-2007, 08:29 PM
  5. Help with password matches
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-10-2007, 05:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •