"If statement" problem in simple code?

Hi,

I am writing a simple homework program however am having some major problems with my "If" Statement and can not figure out why. I want my program to return a different variable if true yet it always says the statement is false. I Have it set up now in the test stage so that if false it prints out the two comparative variables in the Console and even though both variables are the same it still always says its false. Any ideas? Here are my two simple classes below. Thanks. And yes i know it doesn't go through the whole loop, it should not have to because in this test state the first variable it compares should be true.

--

import java.util.StringTokenizer;

public class PhoneList {

private static final String PHONE_BOOK = "mom:860-192-9876,bill:654-987-1234,mary:123-842-1100";

public static String getPhoneNumber(String name) {

//StringBuffer result = new StringBuffer();

StringTokenizer st = new StringTokenizer(PHONE_BOOK, ",:");

String[] tokens = new String[PHONE_BOOK.length()];

int c = 0;

String current = "";

while (st.hasMoreTokens()) {

current = st.nextToken();

tokens[c] = current;

c = c + 1;

}

for (int i = 0; i < c;) {

if (name == tokens) {

return "YES";

}

else {

return tokens + "|" + name;

}

//i=i+1;

}

return "NO";

}

}

--

public class Interface {

public static void main(String[] args) {

String hi = "mom";

System.out.println(PhoneList.getPhoneNumber(hi));

}

}

--

[1629 byte] By [protivakida] at [2007-11-27 4:25:07]
# 1
you can't compare a string (name) to a table of string (tokens)plus: you should use the "code" block tag
calvino_inda at 2007-7-12 9:33:10 > top of Java-index,Java Essentials,Java Programming...
# 2
Alright, Any suggestions on how to do what I want to do? Do I have to put the tokens into a String[] array?
protivakida at 2007-7-12 9:33:10 > top of Java-index,Java Essentials,Java Programming...
# 3

> Alright, Any suggestions on how to do what I want to

> do? Do I have to put the tokens into a String[] array?

For one thing you need to use the equals method to compare objects and not ==.

For the second no. You want to compare Strings it seems so you should compare a String a String (in your code current).

cotton.ma at 2007-7-12 9:33:10 > top of Java-index,Java Essentials,Java Programming...
# 4
A couple of things:Strings should be compared using the equals method and not using ==Use StringTokenizer.countTokens to determine the size of your arrayFor each token that you put into your array, you can match it against your name
thomas.behra at 2007-7-12 9:33:11 > top of Java-index,Java Essentials,Java Programming...
# 5

Ive read the replies and am still confused. I know what the problem is because when you change it from "==" to "=" it pretty much tells u it can't compare. What I don't get is how to go about fixing this. I need to compare my one string to a whole array of Strings that I previously broke down based on what symbols followed each word of the large lump string. Any help will be appreciated. Thanks.

protivakida at 2007-7-12 9:33:11 > top of Java-index,Java Essentials,Java Programming...
# 6
please read the section on this wikibooks page labeled Compare Strings. You can find it [url= http://en.wikibooks.org/wiki/Java_Programming/API/java.lang.String]here[/url]. I think that it will teach you what you need to know.Good luck/Pete
petes1234a at 2007-7-12 9:33:11 > top of Java-index,Java Essentials,Java Programming...
# 7

Hey,

Thanks guys I got that working, my question now is, whats the best way to input a String into the console from the user and have the string stored as a string variable? I know how to do it for an INT and after reading online can do stings with the try / catch method but I want a more simple better way such as the way I do INTs...

import java.util.Scanner;

int choice;

Scanner in = new Scanner(System.in);

choice = in.nextInt();

Any easy way to do this for Strings? Thanks

protivakida at 2007-7-12 9:33:11 > top of Java-index,Java Essentials,Java Programming...
# 8
you ve got "nextLine()" in class scanner, for that
calvino_inda at 2007-7-12 9:33:11 > top of Java-index,Java Essentials,Java Programming...