Results 1 to 3 of 3

Thread: Java Code Help

  1. #1
    baymandernam is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Question Java Code Help

    Hello Everybody, i made this program and im getting an 'error' in the result, when i type in the username it sais its wrong every time even when i type in the one i specified "Dan/dan"
    heres the code:

    import java.util.Scanner;

    public class Main {
    public static void main(String[] args){
    System.out.println("Welcome User, To Access This Program, You must Login First\n");
    System.out.println("Please Enter Your Username: ");
    Scanner scan = new Scanner(System.in);
    String usern = scan.nextLine();
    System.out.println(usern);

    if (usern == "Dan" || usern == "dan"){
    System.out.println("Please Enter Your Password: \n");
    pass();
    }
    else if (usern != "Dan" || usern != "dan") {
    System.out.println("Sorry, That Username Is Not Recognized\n\n");
    main(args);
    }
    else {
    System.out.println("Unknown Input");
    }
    }
    public static void pass() {
    System.out.println("Please Enter The Password:\n\n");
    Scanner pass = new Scanner(System.in);
    String passw = pass.nextLine();
    System.out.println(passw);

    if (passw == "Test" || passw == "test"){
    System.out.println("Access Granted\n\n\n");
    menu();
    }
    else {
    System.out.println("Access Denied \n\n");
    pass();
    }

    }
    static void menu(){
    System.out.println("****************************** ***********\n");
    System.out.println("**** Menu ****\n");
    System.out.println("****************************** ***********\n");
    System.out.println("**** Credits ****\n");
    System.out.println("**** Game ****\n");
    System.out.println("**** Quit ****\n");
    System.out.println("****************************** ***********\n");
    System.out.println("Enter Your Choice: \n");
    Scanner input = new Scanner(System.in);
    String scan = input.nextLine();
    System.out.println(scan);

    if (scan == "Credits" || scan == "credits"){
    System.out.println("This Program Was Made By Daniel Bloom");
    System.out.println("Feel Free To Use Or Change This Program, But Keep This Message Here");
    System.out.println("Thank You");
    menu();
    }
    else if (scan == "Game" || scan == "game"){
    int numSticks = 21;
    System.out.println("Would You Like To Go First? (y/n");
    Scanner choices = new Scanner(System.in);
    String goFirst = choices.nextLine();
    Scanner take = new Scanner(System.in);
    int numToTake = 0;

    while (numSticks > 0){
    if (goFirst.equals("y") || goFirst.equals("Y")){
    System.out.println("There Are " + numSticks + " Sticks");
    System.out.println("How Many Sticks To Take (1 or 2)");
    numToTake = take.nextInt();

    if (numToTake > 2){
    numToTake = 2;
    }
    else if (numToTake < 1){
    numToTake = 1;
    }
    numSticks = numSticks - numToTake;
    if (numSticks <=0){
    System.out.println("You Lose!");
    }
    else {
    if ((numSticks - 2) % 3 == 0 || numSticks - 2 ==0 ){
    numToTake = 1;
    }
    else{
    numToTake = 2;
    }
    System.out.println("Computer Takes " + numToTake + " Sticks");
    numSticks = numSticks - numToTake;

    if (numSticks <= 0){
    System.out.println("You Win!");
    }
    }
    }
    else {
    if ((numSticks - 2) % 3 == 0 || numSticks - 2 == 0){
    numToTake = 1;
    }
    else {
    numToTake = 2;
    }
    System.out.println("Computer Takes " + numToTake + " Sticks");
    numSticks = numSticks - numToTake;

    if (numSticks <= 0){
    System.out.println("You Win!");
    }
    else {
    System.out.println("There Are "+ numSticks +" Sticks");
    System.out.println("How Many Sticks To Take (1 or 2)");
    numToTake = take.nextInt();

    if (numSticks > 2){
    numToTake = 2;
    }
    else if (numToTake < 1){
    numToTake = 1;
    }
    numSticks = numSticks - numToTake;

    if (numSticks <= 0){
    System.out.println("You Lose!");
    }
    }
    }
    }

    }
    if (scan == "Quit" || scan == "quit"){
    System.out.println("Thank You For Using This Program!");
    }
    }
    }


    heres the result:

    Welcome User, To Access This Program, You must Login First

    Please Enter Your Username:
    Dan
    Dan
    Sorry, That Username Is Not Recognized


    Welcome User, To Access This Program, You must Login First

    Please Enter Your Username:
    dan
    dan
    Sorry, That Username Is Not Recognized


    Welcome User, To Access This Program, You must Login First

    Please Enter Your Username:


    as you can see, even though Dan and dan were specified as correct in the code it is telling me they are wrong. Please Help!

    Thank you,

    Dan

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Code Help

    You need to use the equals() method to compare Strings, not the == operator.

    Please edit your post and wrap the code in code tags. BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Java Code Help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  2. Translate Vb.net code into Java code
    By Radu in forum New To Java
    Replies: 5
    Last Post: 04-12-2011, 10:27 AM
  3. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 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
  •