Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 is c++'s != the same in java?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: Wed Mar 01, 2006 7:38 pm   Post subject: is c++'s != the same in java?

Java:

import java.util.*;
import java.io.*;
public class NumberGuess3
{
    public static void main(String[] args)
    {
       int answer;
       String guess     = "";
       String playAgain = "";
       boolean tryAgain = true;
       int count = 1;
       BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
       Random rand = new Random();
       answer = rand.nextInt(99) + 1;   
       System.out.println("Guess the number between 1-100.");
       System.out.println("The number is " + answer);
       while (tryAgain == true)
       {         
         try
         {
            guess = input.readLine();
         }
         catch (Exception e)
         {
         }
         if (Integer.parseInt(guess) == answer)
         {
           if (count <= 3)
             System.out.println("Wow, you must be psychic!");
           if (count > 3 && count <= 6)
             System.out.println("Not bad, better luck next time!");
           if (count > 6 && count <= 10)
                  System.out.println("You need practice!");
           if (count > 10)
             System.out.println("Were you even looking at the keyboard?");
   
           System.out.println("Enter \"yes\" if you want to play again");
           try
           {
                   playAgain = input.readLine();
           }
           catch (Exception e)
           {
                   System.out.println("input error");
           }
           
           if (playAgain != "yes")
             tryAgain = false;
             
           count = 1;                  
         }
         else       
           System.out.println("Guess again noob.");
           count += 1;
       }
     
     }
}


can someone tell me why it doesnt go back to the loop after try again? I know it wont try the whole thing again due to the breadth of the while loop, but it should do the while loop again. Unless the != (does not equal) sign is different in java.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Mar 01, 2006 8:12 pm   Post subject: (No subject)

Java's == and != operators work the way you'd expect... for "native" types. For object types they act referentially.

code:
someString == someString2


Only is true if they're actually the same object. If you wish to test for structural equality, use the equals method.
Andy




PostPosted: Wed Mar 01, 2006 8:35 pm   Post subject: (No subject)

in c++ != and == will return u 0 or 1, in java, true != 1 and false != 0
Justin_




PostPosted: Wed Mar 01, 2006 9:32 pm   Post subject: (No subject)

I'm begining to suspect that the readLine() method does something funky.

observe:

playAgain = input.readLine();

while (playAgain == "yes")
{
execute this
}

okay, so if i type in "yes" without quotes, it should do the while loop right?
Justin_




PostPosted: Wed Mar 01, 2006 9:37 pm   Post subject: (No subject)

Please tell me why it doesnt repeat itself if you type: yes after you guessed the number correct.

Java:

import java.util.*;
import java.io.*;
public class NumberGuess3
{
  public static void main(String[] args)
  {
       int answer;
       String guess     = "";
       String playAgain = "";
       int count = 1;
       BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
       Random rand = new Random();
       
    do   
    {
       answer = rand.nextInt(99) + 1;   
       System.out.println("Guess the number between 1-100.");
       System.out.println("The number is " + answer);
       
       while (true)
       {         
         try
         {
            guess = input.readLine();
         }
         catch (Exception e)
         {
         }
         if (Integer.parseInt(guess) == answer)
         {
           if (count <= 3)
             System.out.println("Wow, you must be psychic!");
           if (count > 3 && count <= 6)
             System.out.println("Not bad, better luck next time!");
           if (count > 6 && count <= 10)
                  System.out.println("You need practice!");
           if (count > 10)
             System.out.println("Were you even looking at the keyboard?");
   
           System.out.println("Enter \"yes\" if you want to play again");
           try
           {
                   playAgain = input.readLine();
           }
           catch (Exception e)
           {
                   System.out.println("input error");
           }
                             
           count = 1;
           break;                      
         }
         else       
           System.out.println("Guess again noob.");
           count += 1;
       }
     
     } while (playAgain == "yes");
  }   
}
Andy




PostPosted: Wed Mar 01, 2006 9:54 pm   Post subject: (No subject)

two suggestions, check if the string you're comparing with "yes" is indeed just "yes" and no endline character, output the length of the string, and check each character. Also, make sure you're using Java 1.50 if you're going to do that, if not you'll have to use string.equals
wtd




PostPosted: Wed Mar 01, 2006 10:17 pm   Post subject: (No subject)

I've given you the answer.

If you wish to strip off any extra spaces (newline or otherwise), use the trim method.
Justin_




PostPosted: Wed Mar 01, 2006 10:39 pm   Post subject: (No subject)

I output the length of the word yes and it returns 3. Also I read that the readLine() method does not include the terminating characters or anything like that when writing to a string variable.

wtd, if you've given the answer then I don't understand it. A trim method will not help in this situation considering the string is just "yes" so there has to be something else wrong with my program.

Whoever can tell me what earns a cookie Very Happy
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Mar 01, 2006 11:25 pm   Post subject: (No subject)

Strings are objects in Java. There's your answer.
Justin_




PostPosted: Wed Mar 01, 2006 11:32 pm   Post subject: (No subject)

oh indeed, thanks so much. When you say equals method I tried the statement with one equal sign lol. But now I understand, thanks.
[Gandalf]




PostPosted: Thu Mar 02, 2006 11:28 pm   Post subject: (No subject)

Justin_ wrote:
hi

Eh? Would I be correct in the assumption that someone has commandeered your account?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: