Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-08-2009, 02:11 AM
Member
 
Join Date: Jan 2009
Posts: 5
Rep Power: 0
jdiesel is on a distinguished road
Default [SOLVED] Ending for loop with a String
Hi:
I am writing a for loop, and I want it to stop when a specific String entered by a user.The problem is it does not stop. I tried the loop using an integer entry instead and the loop does stop. But I need to use a String as a stopping signal.
Thanks in advance for the help.

import java.util.*;

class Roulette
{
public static void main (String [] args)
{
System.out.println("\nWelcome to Roulette!");
System.out.println("First type the name of the players."); System.out.println("When you are done entering their name,
just hit enter a blank line to continue.\n");

String [] player = new String [50];
int [] startMoney = new int [50];

Scanner keyboard = new Scanner(System.in);

for(int i=1; i < 50; i++)
{
System.out.println("What is the Name of Player #"+i+"?");
String s = keyboard.next();
if (s == "no") i = 50;
else
{
player[i] = s; System.out.println("How much money does " + player[i] + " start off
with? ");
startMoney[i] = keyboard.nextInt();
}

}
}
}
~
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-08-2009, 02:48 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default String Comparation
Don't use "==" to compare strings. Use the String method .equals():
Code:
if (s.equals("no"))
  {
   .....
  }
String class & methods:
String (Java Platform SE 6)

String equals method:
String equals method (Java Platform SE 6))

Also, always use curly brackets "{}" after if, else and for statements, even if they're one liners. It will save you a lot of head aches later on.

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-08-2009, 03:00 AM
Member
 
Join Date: Jan 2009
Posts: 5
Rep Power: 0
jdiesel is on a distinguished road
Default
Thanks a lot, that sealed the deal.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-08-2009, 03:11 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Any time
You're welcome. Mark the ppost as solved.

LucK,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Let eclipse warn about a semicolon after an if statement and string == string? foobar.fighter Eclipse 5 01-11-2009 11:12 AM
JAVA: String char removal with nested loop igniteflow New To Java 3 11-28-2008 03:09 AM
Entering a while loop with a not equal to string bri1547 New To Java 9 07-09-2008 08:10 AM
i need assistance with a string triggered loop please! Phobos0001 New To Java 9 11-14-2007 03:44 PM
terminating a while loop with a string tkdvipers New To Java 3 07-10-2007 12:23 AM


All times are GMT +2. The time now is 07:47 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org