Results 1 to 11 of 11
  1. #1
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Error showing up on the programming

    Write a program reads in a series of passwords into an array. Store 10 passwords. After creating the password array, have the reader enter a password and see if it is in the array. Fi it is there, print "Valid password" and if it is not there, print "Invalid password."
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This is what i have so far
    ----------------------------------------------------

    Java Code:
    public class passwordMatch
    {
     public static void main (String [] args){
      String [] myArray = new String[10]; 
      myArray[0] = "jasmine22";
      myArray[1] = "jackson77";
      myArray[2] = "michaelawesome";
      myArray[3] = "whoareyou";
      myArray[4] = "youlookcute";
      myArray[5] = "eggplantcurry";
      myArray[6] = "partygirl888";
      myArray[7] = "dancethenightout";
      myArray[8] = "loveisintheair";
      myArray[9] = "coconutcrocodile";
      System.out.println("Enter a password: ");
      Scanner in = new Scanner(System.in);
      String myString = in.next();
      
       if (myString == String.myArray[]){
        System.out.println("Password Accepted");
        }
       else 
        System.out.println("Password Rejected");
     
    }
    }
    ----------------------------------------------------
    it doesn't compile. It gives me error saying .class expected next to String.myArray[]
    ---------------------------------------------------
    Last edited by JosAH; 04-21-2014 at 10:42 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Error showing up on the programming

    What did you want to express with that line? Should 'myString' be present in that array?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Error showing up on the programming

    If you want to check if the entered string is inside your array, then you have to check each individual entry. You could use a loop to go through your array.

    Did that give you the direction in which you have to go?

    With kind regards,

    Konrad

  4. #4
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error showing up on the programming

    what i am trying to do is : when the user enter a password, if whatever the user enters and what it is stored if it matches, then it should print out "Password Accepted" or else say "Password Rejected"

  5. #5
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error showing up on the programming

    no my professor just gave the questions but no instructions how to do it

  6. #6
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error showing up on the programming

    @ Konrad: why should i use loop? I mean all i want is to do is to compare if all the password stored in the array is equal to the one a user enters.

  7. #7
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error showing up on the programming

    can i just use if statement and compare it? Sorry for asking these questions

  8. #8
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error showing up on the programming

    public static void main (String [] args){
    String [] myArray = new String[10];
    int m;
    for (m=0;m<9;m++){
    myArray[0] = "jasmine22";
    myArray[1] = "jackson77";
    myArray[2] = "michaelawesome";
    myArray[3] = "whoareyou";
    myArray[4] = "youlookcute";
    myArray[5] = "eggplantcurry";
    myArray[6] = "partygirl888";
    myArray[7] = "dancethenightout";
    myArray[8] = "loveisintheair";
    myArray[9] = "coconutcrocodile";
    }
    System.out.println("Enter a password: ");
    Scanner in = new Scanner(System.in);
    String myString = in.next();

    if (myString == myArray[m]){
    System.out.println("Password Accepted");
    }
    else
    System.out.println("Password Rejected");

    }
    }



    it rejects everything i type. it would say "password rejected"

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Error showing up on the programming

    You can't compare two Strings for equality with the == operator; use the equals( ... ) member method instead (this should be in your text book).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Error showing up on the programming

    And check what you have in your loop. Do you want to the foloowing stuff 10 times?
    - Initialize the array
    - Ask for input
    - Check the password with the nth element?
    - Output the result

    So how could you change your code so that
    - the array is only initialized once
    - the user is only asked once to enter the password
    - All elements are checked.

    Maybe another hint: If you have to test multiple things, you could do something like
    - define boolean result = false
    - (multiple times) check if password match. if it matches, set result to true. (And maybe leave the loop)
    - now if the match was success at least one time, result is true. Else it is still false.

    With kind regards,

    Konrad

  11. #11
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error showing up on the programming

    Thanks. I figured it out. Thanks

Similar Threads

  1. Replies: 2
    Last Post: 07-29-2013, 12:16 PM
  2. Replies: 9
    Last Post: 05-28-2013, 10:14 AM
  3. Replies: 6
    Last Post: 05-26-2013, 03:53 AM
  4. showing error in the pop
    By arun9683 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 07-02-2010, 11:53 AM
  5. Error Messages Not Showing Up
    By nvidia in forum Web Frameworks
    Replies: 0
    Last Post: 04-07-2008, 11:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •