Skip to main content

Looping thru confusion

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
iceregent
Offline
Joined: 2011-10-17

I am trying to write in java. I have a program called "NetBeans" to use. I am working on writing a simple program, based upon a challenge tp write a simple program. However, i am having a problem with a while loop, not working as expected. Here is the section of code. The result is that it just loops over and over, regardless on the user input.

import java.util.*;
public class QuaziStraights {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] myHand;
myHand = new int[4];
String mode ="";
//loop to input the hand
while(mode != "1"||mode != "2"){
System.out.print("Would you like to play in manual [1] or in Random [2] mode? ");
mode = input.nextLine();
System.out.println("You typed in " + mode);
}
}
public static Boolean isStraight(int numRank[]){
Boolean ranking = false;
return ranking;
}
Can anyone see plainly why it does not accept the input and exit the loop for the conditions implied?
Thank you

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
OneJumpedUp
Offline
Joined: 2012-04-05

P.S. I forgot, I deliberately reversed the condition, see if you can figure it out :-}

OneJumpedUp
Offline
Joined: 2012-04-05

Hey There, try this

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int[] myHand;
        myHand = new int[4];
        String mode = "";
        //loop to input the hand
        do
        {
            System.out.print("Would you like to play in manual [1] or in Random [2] mode? ");
            mode = input.nextLine();
            System.out.println("You typed in " + mode);
        }
        while( mode.equals( "1" ) || mode.equals( "2" ));
    }

P.S. watch the string comparisons !!!