Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Learining Java and have a question about some code that is not working (help)

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
I am passing the following to this code Howard James Mike. I am trying to read Howard but it is not working Why..

PART THAT DOES NOT WORK
if (myout == "Howard")
{
System.out.println("Hey Howardppp");
}

ENTIRE CODE
public class MyTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int howmany = args.length;
String[] myInfo = new String[howmany];


for (int a = 0 ; a < howmany; a++)
{
myInfo[a] = args[a]; //"Howard";
}


System.out.println(" how many args " + howmany);




for (int i = 0 ; i < howmany; i++)
{
System.out.println("Item" + i + " " + myInfo);

String myout;
myout = myInfo;
// this part does not work why
if (myout == "Howard")
{
System.out.println("Hey Howardppp");
}
//System.out.println("myout=" + myout);
}

System.out.println("end");

}

}
 
Hi

The classic beginner mistake. [tt]==[/tt] checks equality of pointers. Use [tt]String.equals()[/tt] to check equality of values :
Code:
[b]if[/b] [teal]([/teal]myout[teal].[/teal][COLOR=darkgoldenrod]equals[/color][teal]([/teal][green][i]"Howard"[/i][/green][teal]))[/teal] [teal]{[/teal]
  System[teal].[/teal]out[teal].[/teal][COLOR=darkgoldenrod]println[/color][teal]([/teal][green][i]"Hey Howardppp"[/i][/green][teal]);[/teal]
[teal]}[/teal]

[gray]// or with null-safe Yoda condition[/gray]
[b]if[/b] [teal]([/teal][green][i]"Howard"[/i][/green][teal].[/teal][COLOR=darkgoldenrod]equals[/color][teal]([/teal]myout[teal]))[/teal] [teal]{[/teal]
  System[teal].[/teal]out[teal].[/teal][COLOR=darkgoldenrod]println[/color][teal]([/teal][green][i]"Hey Howardppp"[/i][/green][teal]);[/teal]
[teal]}[/teal]

Next time please indent your code and post it between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top