By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,024 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,024 IT Pros & Developers. It's quick & easy.

GUI problems in Java. Pls help. Thanks

P: 35
I have made this piece of code where the output should display a dialogue box asking for a password. Then if the user enters it correctly it displays "Valid" or else it displays "Not Valid". However its only dispalying the dialogue box to enter the password. It isn't displaying whether its valid or not. What have i made wrong? Thanks a lot.

PS: I'm still a beginner in java

This is the piece of code i have written:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;


public class GeographyQuiz_Menu
{


public static void main(String[] args)
{
JFrame jFrame;
jFrame = new JFrame();



JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
JOptionPane.showMessageDialog(null, "Good Luck");


String yourChoice;
char choice;
int i, choice1;

int Password;
Scanner keyboard = new Scanner(System.in);
JOptionPane.showInputDialog(jFrame, "Enter the Password");

Password = keyboard.nextInt();

if (Password == 123)
{
JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");

}
else
{
JOptionPane.showMessageDialog(null, "Invalid Password. Try Again.");

}
}
}
Dec 15 '07 #1
Share this Question
Share on Google+
11 Replies


P: 27
here is ur problem:
Password = keyboard.nextInt();
u better use the wrapper class Integer which has the function parseInt. Never use a scanner with dialog boxes, it wont work.
It takes a string and convert it into an integer for maths manipulations.
anyway try this and i hope its what u need.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class GeographyQuiz_Menu
{


public static void main(String[] args)
{
JFrame jFrame;
jFrame = new JFrame();



JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
JOptionPane.showMessageDialog(null, "Good Luck");


String yourChoice;
char choice;
int i, choice1;

int Password;
String passString;
Scanner keyboard = new Scanner(System.in);

passString = JOptionPane.showInputDialog("Enter the Password");


//Password = passString.nextInt();
Password = Integer.parseInt(passString);


if (Password == 123)
{
JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");

}
else
{
JOptionPane.showMessageDialog(null, "Invalid Password. Try Again.");

}
}
}
Dec 15 '07 #2

P: 35
Thanks a lot! You'r Great! :-)
Dec 16 '07 #3

P: 27
Thanks a lot! You'r Great! :-)
ur welcome anytime ;)
Dec 16 '07 #4

P: 35
I have another problem now. :-( i have to read a textfile from Java called platetectonics.txt I managed to call it from Java using the buffered reader, but i wish to display this textfile in a GUI format, when i call it from java. How can i do this? Thanks a lot. :-)

This is the code i've written:

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_Menu
{


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;
String yourChoice;
int Password;
String passString;



passString = JOptionPane.showInputDialog("Enter the Password");


//Password = passString.nextInt();
Password = Integer.parseInt(passString);


if (Password == 123)
{
JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");
JOptionPane.showMessageDialog(null, "Welcome!\nThis is a Geography Quiz\nChoose from the following Menu:");


JOptionPane.showMessageDialog(null, "1. Plate Tectonics\n2. Rivers\n3. Rocks\n4. Quit");

passString = JOptionPane.showInputDialog("Enter your choice");
choice1 = Integer.parseInt(passString);

// Convert variable from string to integer
if(choice1<1 || choice1>4)
{
JOptionPane.showMessageDialog(null, "This is an invalid choice");
}

switch(choice1)
{
case 1: JOptionPane.showMessageDialog(null,"You are taking the Plate Tectonics Quiz");
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt")));

while (s.hasNext()) {
s.useDelimiter(",\\s*");
JOptionPane.showMessageDialog(null,s.next());
}
} finally {
if (s != null)
s.close();

break;

}
}

}

else
{
JOptionPane.showMessageDialog(null, "Invalid Password. Try Again");
}

}
}
Dec 16 '07 #5

P: 27
its already workin i donno what else u need. now u can show stuff in the file in the dialog box.
its only JOptionPane.showMessageDialog.... enough to do the job

u can also show each line in a new box by removing the useDelimiter() statement
Dec 16 '07 #6

P: 35
its already workin i donno what else u need. now u can show stuff in the file in the dialog box.
its only JOptionPane.showMessageDialog.... enough to do the job

u can also show each line in a new box by removing the useDelimiter() statement
But the program is stopping when it shows a dialogue box saying that i have chosen the Plate Tectonics Quiz. After that i want the program to display the questions found in a textfile (platetectonics.txt) in a GUI box. This part isn't working correctly. I think that there's something missing in it.

switch(choice1)
{
case 1: JOptionPane.showMessageDialog(null,"You are taking the Plate Tectonics Quiz");
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt")));

while (s.hasNext()) {
s.useDelimiter(",\\s*");
JOptionPane.showMessageDialog(null,s.next());
}
} finally {
if (s != null)
s.close();

break;

}
Dec 16 '07 #7

P: 27
im still confused with what u wanna do. thats what i've guessed:

this gets u each line(question) in the txt file.

JOptionPane.showMessageDialog(null,s.nextLine());
using the method nextLine instead of next() which gets a single string
Dec 17 '07 #8

P: 35
Yeah you were totally right. It worked. Thanks a lot for your help. I really, really appreciate it. :-)
Dec 17 '07 #9

P: 27
thats good news.
ur welcome :)

would u mind sending me the assignment?? coz i wanna do it as a self challenge
good luck
Dec 18 '07 #10

10K+
P: 13,264
Guys let me remind you all of the need to stick to posting guidelines when posting in the forums.
Posting code without code tags and spoon feeding are not allowed.

Merry Christmas.
Dec 18 '07 #11

P: 27
Guys let me remind you all of the need to stick to posting guidelines when posting in the forums.
Posting code without code tags and spoon feeding are not allowed.

Merry Christmas.
ok man sry for that. just wanted to help.
merry christmas :)
Dec 18 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.