Pls help with a project. About doing passwords. Thanks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saytri
    New Member
    • Dec 2007
    • 35

    Pls help with a project. About doing passwords. Thanks

    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.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    I think you need to rewrite your game code. A program should have only one main method, and even if you managed to glue these two classes together, going from a Swing GUI to enter a password to a console-based game is awkward, clunky.

    Comment

    • saytri
      New Member
      • Dec 2007
      • 35

      #3
      Originally posted by BigDaddyLH
      I think you need to rewrite your game code. A program should have only one main method, and even if you managed to glue these two classes together, going from a Swing GUI to enter a password to a console-based game is awkward, clunky.
      Thanks for your reply. But how do you call a class from the main class? I'm a bit new in java (still a student:- first year to java) Thanks a lot.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by saytri
        Thanks for your reply. But how do you call a class from the main class? I'm a bit new in java (still a student:- first year to java) Thanks a lot.
        There's no "main class" in Java. I think you are asking how classes can be used together. You already know how. Look at your class PasswordDemo. It is using the classes String, JPanel, JFrame, JPasswordField, JLabel, FlowLayout, JButton, JOptionPane, Arrays, UIManager, SwingUtilities.

        Comment

        • saytri
          New Member
          • Dec 2007
          • 35

          #5
          Thanks. But since i'm very new to Java, the part where it outputs the password, i haven't written it. I found it in the Internet. My problem is how to call the PasswordDemo class from the Geography_menu class. Because in the output i have to display first the part of the login password, and then if the user types it correctly it would display the menu(i.e the geography menu class). Pls help me do that command part because i'm really stuck.

          Thanks a lot for being helpful.

          Comment

          • neuro11
            New Member
            • Dec 2007
            • 3

            #6
            Do u hav java book or tutorial or java doc?? if not, u should collect those first..because as a new to java u hav to first know the very basic things..before going for solving problems.Withou t asking so many times u should learn things first Codin will b just a matter of time then......

            wish u good luck

            Comment

            Working...