• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner question about arrays - Comparing to String  RSS feed

 
Mike Johnston
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys.. I have a beginner's question here. I'm trying to compare something in a String[] to a String.

For example: I have a win/loss/tie thing for a sports team and I enter "W" for a win and "L" for a loss in String[] a.

To see if it matches I've attempted to:



And no matter what I type it's giving me 0/0/0.

I know it's probably a silly mistake but I'm really new to Java. Any help would be great.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== tests for identity. The .equals() method tests for equality.

Don't worry, 100% of novice Java developers makes this mistake at the start.
 
Mike Johnston
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:== tests for identity. The .equals() method tests for equality.


You saved me! Thank you!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Welcome to the Ranch!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Don't worry, 100% of novice Java developers makes this mistake at the start.


And 99.9% of experienced Java developers still make that mistake occasionally after too many caffeine-fueled late nights trying to meet an arbitrary but immovable deadline.
 
Mike Johnston
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question to add on to this if I could get some help heh.. I'm going to add a "last 10 games" streak type thing to the end and I've got:



First of all does this look okay? And secondly how would I call that? Since there are 2 "variables" in the getStreak.
 
Pablo Reyes
Greenhorn
Posts: 7
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:== tests for identity. The .equals() method tests for equality.

Don't worry, 100% of novice Java developers makes this mistake at the start.


That's correct, but did you undestand why?

The "==" operator in Java is used to compare the actual contents of a variables, ie. the bit pattern. What does this mean?

A String is an object, so a[i] will be some reference variable whose bit pattern is the "direction" of the actual object in the heap (a String object, with an "W", "L" or "T"). So when you call a[i], don't think that it will be "replaced" by the content of the String objects which is being referenced, instead, it will be "replaced" with some direction to get to the object.



That code means:
1 - Create a String object in the heap with the content "hi"
2 - Create a reference variable a
3 - Fill a with some way to get to the object created in step 1 (lets say a = 01001001110010101010101)
4 - Create a reference variable b
5 - Fill b with the same bit pattern as a (so b = 01001001110010101010101)
6 - Check if the bit pattern of a and b are the same

So, what you are comparing there is not the content of the object, but the content of the reference variable.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really have no clue what exactly you intend with your "last 10 games streak," or what that method is supposed to do. Have you tested it? Does it produce the results you want? That's you're first guide to whether you're on the right track.

As for how you'd call it, you call it just like you call any other method: whatever(arg1, arg2, ... argN);
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind.
 
Mike Johnston
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really know how to explain what's going on in my head when I type that lol. Essentially I'm trying to take the last 10 games from the array, and see how many in a row were the same, so like win, win, loss, win, win, win would be 3 wins in a row. Also if the game has for example, 13 games in it, I need the last 10.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you're taking the first 10, not the last 10. (Assuming that array[0] means the first score and not the last score, that is.) You're going to have to do some arithmetic to find out where in the input array you should start from, which will make your loop a little bit more complicated.
 
Mike Johnston
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made progress.. I can get it to reprint the entire record. Is there a quick way to jump to the end of the array, and take the last 10?
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is. It's just simple arithmetic. Don't start at entry zero, but do that simple arithmetic to figure out where to start instead. You know how to find the length of an array, right? You need that value as part of the arithmetic.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Johnston wrote:I don't really know how to explain what's going on in my head when I type that lol. Essentially I'm trying to take the last 10 games from the array, and see how many in a row were the same, so like win, win, loss, win, win, win would be 3 wins in a row. Also if the game has for example, 13 games in it, I need the last 10.


Before you even think about writing a single line of Java code for that, make sure you can explain how to do it--precisely and in very simple steps--without regard to Java. You best be is to write it down. Don't be afraid to use variables, in the algebraic sense, they'll translate readily into Java. Once you have it written down and can step through it manually and make it work, turn it into Java. If you find you can't easily turn one of your steps into Java, then you probably need to break that step down even further. That may (or may not) end up translating to a separate method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!