Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the quiz i have to do a password and a login. I managed to do the password but when i tried to join this to the whole program its not working. This is the main program:
import java.util.*;
import java.io.*;
import java.util.Scann er;
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;
import java.util.Array s;
public class GeographyQuiz_M enu
{
public static void main (String args[])
{
String yourChoice;
char choice;
int i, choice1;
Scanner keyboard = new Scanner(System. in);
i = 0;
while (i<=6)
{
// Input Menu
System.out.prin tln(" ");
System.out.prin tln("Welcome!") ;
System.out.prin tln("This is a Geography Quiz");
System.out.prin tln("Choose from the following Menu:");
System.out.prin tln("1. Plate Tectonics");
System.out.prin tln("2. Rivers");
System.out.prin tln("3. Rocks");
System.out.prin tln("4. Quit");
System.out.prin t("Please enter your choice: ");
yourChoice = keyboard.next() ; // Enter choice
System.out.prin tln(" ");
// Validate Choice
choice1=Integer .parseInt(yourC hoice); // Convert variable from string to integer
if(choice1<1 || choice1>4)
{
System.out.prin tln("This is an invalid choice");
}
switch(choice1)
{
case 1: System.out.prin tln("You are taking the Plate Tectonics Quiz");
Scanner s = null;
try {
s = new Scanner(new BufferedReader( new FileReader("pla te_tectonics.tx t")));
while (s.hasNext()) {
s.useDelimiter( ",\\s*");
System.out.prin tln(s.next());
}
} finally {
if (s != null)
s.close();
break;
}
case 2: System.out.prin tln("You are taking the River Quiz");
Scanner b = null;
try {
b = new Scanner(new BufferedReader( new FileReader("riv ers.txt")));
while (b.hasNext()) {
b.useDelimiter( ",\\b*");
System.out.prin tln(b.next());
}
} finally {
if (b != null) {
b.close();
}
break;
}
case 3: System.out.prin tln("You are taking the Rocks Quiz");
Scanner c = null;
try {
c = new Scanner(new BufferedReader( new FileReader("roc ks.txt")));
while (c.hasNext()) {
c.useDelimiter( ",\\c*");
System.out.prin tln(c.next());
}
} finally {
if (c != null) {
c.close();
}
break;
}
}
}
}
}
And this is the part where it does the login password.
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;
import java.util.Array s;
/* PasswordDemo.ja va requires no other files. */
public class PasswordDemo extends JPanel
implements ActionListener {
private static String OK = "ok";
private static String HELP = "help";
private JFrame controllingFram e; //needed for dialogs
private JPasswordField passwordField;
public PasswordDemo(JF rame f) {
//Use the default FlowLayout.
controllingFram e = f;
//Create everything.
passwordField = new JPasswordField( 10);
passwordField.s etActionCommand (OK);
passwordField.a ddActionListene r(this);
JLabel label = new JLabel("Enter the password: ");
label.setLabelF or(passwordFiel d);
JComponent buttonPane = createButtonPan el();
//Lay out everything.
JPanel textPane = new JPanel(new FlowLayout(Flow Layout.TRAILING ));
textPane.add(la bel);
textPane.add(pa sswordField);
add(textPane);
add(buttonPane) ;
}
protected JComponent createButtonPan el() {
JPanel p = new JPanel(new GridLayout(0,1) );
JButton okButton = new JButton("OK");
JButton helpButton = new JButton("Help") ;
okButton.setAct ionCommand(OK);
helpButton.setA ctionCommand(HE LP);
okButton.addAct ionListener(thi s);
helpButton.addA ctionListener(t his);
p.add(okButton) ;
p.add(helpButto n);
return p;
}
public void actionPerformed (ActionEvent e) {
String cmd = e.getActionComm and();
if (OK.equals(cmd) ) { //Process the password.
char[] input = passwordField.g etPassword();
if (isPasswordCorr ect(input)) {
JOptionPane.sho wMessageDialog( controllingFram e,
"Success! You typed the right password.");
} else {
JOptionPane.sho wMessageDialog( controllingFram e,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERR OR_MESSAGE);
}
//Zero out the possible password, for security.
Arrays.fill(inp ut, '0');
passwordField.s electAll();
resetFocus();
} else { //The user has asked for help.
JOptionPane.sho wMessageDialog( controllingFram e,
"Ask the teacher for the password");
}
}
/**
* Checks the passed-in array against the correct password.
* After this method returns, you should invoke eraseArray
* on the passed-in array.
*/
private static boolean isPasswordCorre ct(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'g', 'e', 'o' };
if (input.length != correctPassword .length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword );
}
//Zero out the password.
Arrays.fill(cor rectPassword,'0 ');
return isCorrect;
}
//Must be called from the event dispatch thread.
protected void resetFocus() {
passwordField.r equestFocusInWi ndow();
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGU I() {
//Create and set up the window.
JFrame frame = new JFrame("Passwor dDemo");
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
//Create and set up the content pane.
final PasswordDemo newContentPane = new PasswordDemo(fr ame);
newContentPane. setOpaque(true) ; //content panes must be opaque
frame.setConten tPane(newConten tPane);
//Make sure the focus goes to the right component
//whenever the frame is initially given the focus.
frame.addWindow Listener(new WindowAdapter() {
public void windowActivated (WindowEvent e) {
newContentPane. resetFocus();
}
});
//Display the window.
frame.pack();
frame.setVisibl e(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities. invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put(" swing.boldMetal ", Boolean.FALSE);
createAndShowGU I();
}
});
}
}
Can someone pls tell me how to join them to make them work in the correct way. Thanks a lot.
import java.util.*;
import java.io.*;
import java.util.Scann er;
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;
import java.util.Array s;
public class GeographyQuiz_M enu
{
public static void main (String args[])
{
String yourChoice;
char choice;
int i, choice1;
Scanner keyboard = new Scanner(System. in);
i = 0;
while (i<=6)
{
// Input Menu
System.out.prin tln(" ");
System.out.prin tln("Welcome!") ;
System.out.prin tln("This is a Geography Quiz");
System.out.prin tln("Choose from the following Menu:");
System.out.prin tln("1. Plate Tectonics");
System.out.prin tln("2. Rivers");
System.out.prin tln("3. Rocks");
System.out.prin tln("4. Quit");
System.out.prin t("Please enter your choice: ");
yourChoice = keyboard.next() ; // Enter choice
System.out.prin tln(" ");
// Validate Choice
choice1=Integer .parseInt(yourC hoice); // Convert variable from string to integer
if(choice1<1 || choice1>4)
{
System.out.prin tln("This is an invalid choice");
}
switch(choice1)
{
case 1: System.out.prin tln("You are taking the Plate Tectonics Quiz");
Scanner s = null;
try {
s = new Scanner(new BufferedReader( new FileReader("pla te_tectonics.tx t")));
while (s.hasNext()) {
s.useDelimiter( ",\\s*");
System.out.prin tln(s.next());
}
} finally {
if (s != null)
s.close();
break;
}
case 2: System.out.prin tln("You are taking the River Quiz");
Scanner b = null;
try {
b = new Scanner(new BufferedReader( new FileReader("riv ers.txt")));
while (b.hasNext()) {
b.useDelimiter( ",\\b*");
System.out.prin tln(b.next());
}
} finally {
if (b != null) {
b.close();
}
break;
}
case 3: System.out.prin tln("You are taking the Rocks Quiz");
Scanner c = null;
try {
c = new Scanner(new BufferedReader( new FileReader("roc ks.txt")));
while (c.hasNext()) {
c.useDelimiter( ",\\c*");
System.out.prin tln(c.next());
}
} finally {
if (c != null) {
c.close();
}
break;
}
}
}
}
}
And this is the part where it does the login password.
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;
import java.util.Array s;
/* PasswordDemo.ja va requires no other files. */
public class PasswordDemo extends JPanel
implements ActionListener {
private static String OK = "ok";
private static String HELP = "help";
private JFrame controllingFram e; //needed for dialogs
private JPasswordField passwordField;
public PasswordDemo(JF rame f) {
//Use the default FlowLayout.
controllingFram e = f;
//Create everything.
passwordField = new JPasswordField( 10);
passwordField.s etActionCommand (OK);
passwordField.a ddActionListene r(this);
JLabel label = new JLabel("Enter the password: ");
label.setLabelF or(passwordFiel d);
JComponent buttonPane = createButtonPan el();
//Lay out everything.
JPanel textPane = new JPanel(new FlowLayout(Flow Layout.TRAILING ));
textPane.add(la bel);
textPane.add(pa sswordField);
add(textPane);
add(buttonPane) ;
}
protected JComponent createButtonPan el() {
JPanel p = new JPanel(new GridLayout(0,1) );
JButton okButton = new JButton("OK");
JButton helpButton = new JButton("Help") ;
okButton.setAct ionCommand(OK);
helpButton.setA ctionCommand(HE LP);
okButton.addAct ionListener(thi s);
helpButton.addA ctionListener(t his);
p.add(okButton) ;
p.add(helpButto n);
return p;
}
public void actionPerformed (ActionEvent e) {
String cmd = e.getActionComm and();
if (OK.equals(cmd) ) { //Process the password.
char[] input = passwordField.g etPassword();
if (isPasswordCorr ect(input)) {
JOptionPane.sho wMessageDialog( controllingFram e,
"Success! You typed the right password.");
} else {
JOptionPane.sho wMessageDialog( controllingFram e,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERR OR_MESSAGE);
}
//Zero out the possible password, for security.
Arrays.fill(inp ut, '0');
passwordField.s electAll();
resetFocus();
} else { //The user has asked for help.
JOptionPane.sho wMessageDialog( controllingFram e,
"Ask the teacher for the password");
}
}
/**
* Checks the passed-in array against the correct password.
* After this method returns, you should invoke eraseArray
* on the passed-in array.
*/
private static boolean isPasswordCorre ct(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'g', 'e', 'o' };
if (input.length != correctPassword .length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword );
}
//Zero out the password.
Arrays.fill(cor rectPassword,'0 ');
return isCorrect;
}
//Must be called from the event dispatch thread.
protected void resetFocus() {
passwordField.r equestFocusInWi ndow();
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGU I() {
//Create and set up the window.
JFrame frame = new JFrame("Passwor dDemo");
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
//Create and set up the content pane.
final PasswordDemo newContentPane = new PasswordDemo(fr ame);
newContentPane. setOpaque(true) ; //content panes must be opaque
frame.setConten tPane(newConten tPane);
//Make sure the focus goes to the right component
//whenever the frame is initially given the focus.
frame.addWindow Listener(new WindowAdapter() {
public void windowActivated (WindowEvent e) {
newContentPane. resetFocus();
}
});
//Display the window.
frame.pack();
frame.setVisibl e(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities. invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put(" swing.boldMetal ", Boolean.FALSE);
createAndShowGU I();
}
});
}
}
Can someone pls tell me how to join them to make them work in the correct way. Thanks a lot.
Comment