473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GUI problems in Java. Pls help. Thanks

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
11 1904
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
saytri
35
Thanks a lot! You'r Great! :-)
Dec 16 '07 #3
Thanks a lot! You'r Great! :-)
ur welcome anytime ;)
Dec 16 '07 #4
saytri
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
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
saytri
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
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
saytri
35
Yeah you were totally right. It worked. Thanks a lot for your help. I really, really appreciate it. :-)
Dec 17 '07 #9
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
r035198x
13,262 8TB
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
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

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

Similar topics

0
by: Andy | last post by:
Hi all, Custom taglibs are driving me nuts! I'm trying to install a sample from the book I'm learning from, and it just falls over. Here's my class, which compiles without any problems: ...
2
by: Steven J Sobol | last post by:
This is driving me insane. Previously J2SE 1.4.2/NB 3.51 worked on my week-old Dell Inspiron Windows XP laptop. I figure it's something I downloaded from Windows Update that caused NB to stop...
0
by: Si | last post by:
Hi, I am having trouble with an install of Oracle 9i Release 2 on Linux Mandrake 9.1. I have 512 MB DDR RAM and a Duron 850 CPU. The install appeared to run fairly seamlessly except for a...
1
by: black hawk | last post by:
Hello, i have a big problems, to install oracle 9i v9.2 Server to my "Windows XP" with Service Pack 2 and all security and other updates from Microsoft. The "Universall Installer" (Standard...
0
by: black hawk | last post by:
Hello, i have a big problems, to install oracle 9i v9.2 Server to my "Windows XP" with Service Pack 2 and all security and other updates from Microsoft. The "Universall Installer" (Standard...
3
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm...
7
by: Grant | last post by:
Ok I have XP Pro and recently upgraded to SP2 but after doing so one of my favorite webpages to check daily does not respond the same as before. It used to be (pre SP2) that I could when visiting...
7
by: David Laub | last post by:
I've also posted this issue to a Sun/java formum, but since it appears to be an integration issue, this may be the better place to posr: I have written a dot net/c# Web Services doesn't fully...
8
by: Mark Fink | last post by:
I try to port a server application to Jython. At the moment I use Jython21\Lib\socket.py Currently I do face problems with casting the string "localhost" to the desired value:...
0
by: KiranJyothi | last post by:
Hello everybody, I installed JCreatorI in c:/program files/java and once I am trying to compile my java programs, I am getting this thing : Error : Invalid path,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.