10 Replies - 1519 Views - Last Post: 25 June 2012 - 11:52 PM Rate Topic: -----

#1 Best_man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 12

while loop will not stop

Posted 21 June 2012 - 10:13 PM

so im trying to use a while loop and when i type in no for the input from the reader it still skips by the
"if( "no" == input )" and gos to yes ... any ideas?

 public void play()
    {
        String input = " ";
        boolean playing = true ;
        {
            while (playing == true){
                    dealCard();
                    showHand();
                    System.out.print(" Still want to play? ");
                    input = reader.getInput();
                    if( "no" == input ) {
                        System.out.print(" Game is done ");
                        playing = false;
                        hand.clear(); }
                    if( "yes" == input){
                         System.out.print(" your card " + 'n');
                       playing = true;}  
        }
    }    
  


or its not even seeing the if?

Is This A Good Question/Topic? 0
  • +

Replies To: while loop will not stop

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: while loop will not stop

Posted 21 June 2012 - 10:25 PM

You should switch the values. The one being compared should be on the left side and the value you want to compare it with should be on the right side. Oh, and you should probably use .equals, not ==. The former checks for values, and the the second one checks if they both come from the same reference.

See:
String fruit1 = "apple";
String fruit2 = "apple";
if(fruit1==fruit2)
{
    System.out.println("They're not the same apple!");
}
if(fruit1.equals(fruit2)){
    System.out.println("They're the same apple!");
}

//Will print
// They're not the same apple!
// They're the same apple!


Was This Post Helpful? 4
  • +
  • -

#3 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: while loop will not stop

Posted 21 June 2012 - 10:34 PM

I forgot to add, if you add this to the bottom of the code:
fruit2 = fruit1;
	if(fruit1==fruit2)
	{
	    System.out.println("They're the same apple now!");
	}



That would print the message indicated. That's all. :)
Was This Post Helpful? 2
  • +
  • -

#4 Best_man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 12

Re: while loop will not stop

Posted 21 June 2012 - 10:49 PM

ahh thanks i switched to a if else list but that .equals really helped out thanks.
Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: while loop will not stop

Posted 21 June 2012 - 11:06 PM

No problem! It is a very important lesson in Java and a common mistake often made in this language. Good luck. ^^
Was This Post Helpful? 0
  • +
  • -

#6 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Re: while loop will not stop

Posted 22 June 2012 - 02:01 AM

View PostfromTheSprawl, on 22 June 2012 - 10:55 AM, said:

You should switch the values. The one being compared should be on the left side and the value you want to compare it with should be on the right side. Oh, and you should probably use .equals, not ==. The former checks for values, and the the second one checks if they both come from the same reference.

See:
String fruit1 = "apple";
String fruit2 = "apple";
if(fruit1==fruit2)
{
    System.out.println("They're not the same apple!");
}
if(fruit1.equals(fruit2)){
    System.out.println("They're the same apple!");
}

//Will print
// They're not the same apple!
// They're the same apple!



Hey thnx for this,i never knew that there was a difference between the two.But actually i am a bit confused now.Let me see if i got this correctly.When i use '.equals' the values of both the strings ate compared ie.'apple' and 'apple' so for fruit1.equals(fruit2) i get the answer as true.But i didnt not get the '==' part clearly.What does 'come from the same reference' mean?? In the example you game fruit1 has reference to apple and fruit2 also has reference to 'apple' but not the same one as fruit1.So what will fruit1==fruit2 return?? Will it return false??

I actually tried out a small program:
public class Demo 
{
	public static void main(String args[])
	{
		String one="String 1";
		String two ="String 1";
		if(one==two)
		{
			System.out.println("Entered if");
		}
		
	}
}

But in the if statement whether i write one.equals(two) or one==two it always enters the if loop.
Thanks for your help.

This post has been edited by Posiedon: 22 June 2012 - 02:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: while loop will not stop

Posted 22 June 2012 - 03:23 AM

*
POPULAR

A couple clarifications.

In the case of:

int a = 3;
int b = 5;
int c = 3;

the statements:

( a == b )
( b == a )
( a == c )
( c == a )

will result in false, false, true, true, respectively. Order in the comparison of primitives does not matter. Therefore, the '==' operator is commutative.

It is easy in Java for beginners to think of Strings as primitives (like int, char, float, etc.), because the language has given us shortcuts that cause Strings to 'look' and 'act' like primitives. Strings in Java ARE NOT primitives. Strings in Java are immutable OBJECTS. (Immutable means that they can't change.) One of the ways to be reminded of this is to recognize that the type name String is capitalized, meaning that it's a class in Java. Primitive type names are not capitalized to indicate that they're not a class object.

Comparing typical Java objects: When you compare OBJECTS in Java using the '==' operator, you're comparing one object's beginning memory location to the other object's beginning memory location. fromTheSprawl described this as coming "from the same reference." I'll explain why in a sec.

However, to potentially save memory and time, the JVM pools String constants or literals. The common term in computer science for 'pooling' is 'interning'. String constants or literals are created using the shortcuts

String string1 = "String1";
String string2 = "String1";
String string3 = string1 + string2;

The JVM recognizes that string1 and string2 represent the same 'value' and will store only one "String1" to be used by both string1 and string2. Both string1 and string2 will 'refer' to the same place in memory, hence fromTheSprawl's use of the word reference.

Because of String pooling or interning, string1 == string2 will evaluate to 'true'. The beginning memory locations of both objects are the same. string1.equals( string2 ) will also evaluate to true but because the actual objects are the same.

I mentioned that String pooling is only applied to String literals or constants. Creating a String object using a String constructor will force the JVM to create separate String objects. The two statements,

String string4 = new String( "String1" );
String string5 = new String( "String1" );

creates 2 String objects, both "String1," and stores them at different memory locations. The expression

string4 == string5

will be 'false'.

Hope this helps.

This post has been edited by GregBrannon: 22 June 2012 - 03:25 AM

Was This Post Helpful? 7
  • +
  • -

#8 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Re: while loop will not stop

Posted 22 June 2012 - 06:25 AM

View PostGregBrannon, on 22 June 2012 - 03:53 PM, said:

A couple clarifications.

In the case of:

int a = 3;
int b = 5;
int c = 3;

the statements:

( a == b )
( b == a )
( a == c )
( c == a )

will result in false, false, true, true, respectively. Order in the comparison of primitives does not matter. Therefore, the '==' operator is commutative.

It is easy in Java for beginners to think of Strings as primitives (like int, char, float, etc.), because the language has given us shortcuts that cause Strings to 'look' and 'act' like primitives. Strings in Java ARE NOT primitives. Strings in Java are immutable OBJECTS. (Immutable means that they can't change.) One of the ways to be reminded of this is to recognize that the type name String is capitalized, meaning that it's a class in Java. Primitive type names are not capitalized to indicate that they're not a class object.

Comparing typical Java objects: When you compare OBJECTS in Java using the '==' operator, you're comparing one object's beginning memory location to the other object's beginning memory location. fromTheSprawl described this as coming "from the same reference." I'll explain why in a sec.

However, to potentially save memory and time, the JVM pools String constants or literals. The common term in computer science for 'pooling' is 'interning'. String constants or literals are created using the shortcuts

String string1 = "String1";
String string2 = "String1";
String string3 = string1 + string2;

The JVM recognizes that string1 and string2 represent the same 'value' and will store only one "String1" to be used by both string1 and string2. Both string1 and string2 will 'refer' to the same place in memory, hence fromTheSprawl's use of the word reference.

Because of String pooling or interning, string1 == string2 will evaluate to 'true'. The beginning memory locations of both objects are the same. string1.equals( string2 ) will also evaluate to true but because the actual objects are the same.

I mentioned that String pooling is only applied to String literals or constants. Creating a String object using a String constructor will force the JVM to create separate String objects. The two statements,

String string4 = new String( "String1" );
String string5 = new String( "String1" );

creates 2 String objects, both "String1," and stores them at different memory locations. The expression

string4 == string5

will be 'false'.

Hope this helps.


tyvm man...the concept is very clear to me now.... :)
Was This Post Helpful? 0
  • +
  • -

#9 busyme12srv  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 18-June 12

Re: while loop will not stop

Posted 25 June 2012 - 04:15 AM

Instead of : if( "no" == input ) {
enter if(input.equals("no")) ...
Was This Post Helpful? 1
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,685
  • Joined: 19-March 11

Re: while loop will not stop

Posted 25 June 2012 - 12:16 PM

View Postbusyme12srv, on 25 June 2012 - 06:15 AM, said:

Instead of : if( "no" == input ) {
enter if(input.equals("no")) ...



There is one advantage to doing it the other way around, like this:

if ("no".equals(input)) { 
}


This one won't throw an NPE if input happens to be null.
Was This Post Helpful? 0
  • +
  • -

#11 Best_man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 12

Re: while loop will not stop

Posted 25 June 2012 - 11:52 PM

thanks for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1