Results 1 to 3 of 3
Thread: Problem with a password
- 12-27-2007, 10:26 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
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"); } } }
- 12-27-2007, 11:03 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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 11:06 AM.
Website: Learn Java by Examples
- 12-27-2007, 11:06 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
Password System help
By quickfingers in forum New To JavaReplies: 1Last Post: 06-23-2008, 06:18 PM -
Asking for password from a Servlet
By Java Tip in forum Java TipReplies: 0Last Post: 01-27-2008, 08:05 PM -
add password to folder
By ismailsaleh in forum AWT / SwingReplies: 1Last Post: 01-08-2008, 05:46 AM -
How would I transmit my password through java
By silvia in forum NetworkingReplies: 1Last Post: 07-30-2007, 07:29 PM -
Help with password matches
By Albert in forum AWT / SwingReplies: 1Last Post: 07-10-2007, 04:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks