3 Replies - 717 Views - Last Post: 12 September 2013 - 05:14 PM Rate Topic: -----

#1 GoldenFyre   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-September 13

Breaking out of this loop? (Boolean won't work?)

Posted 12 September 2013 - 08:52 AM

What am I missing here? I made a topic on this yesterday and all I could see was that I should use Booleans. I really didn't understand anything else, but in the code I'm just messing around with declaring stuff and if/else statements and do while loops. What do I have to fix to make this code work? Please be specific....

//Nasim Jallad
//Personal Project
//Your Own Adventure Story

import java.util.Scanner;  // needed for Scanner
import java.text.*;

public class YourOwnStory
{

	// Create a single shared Scanner for keyboard input - For input usage (stand alone)
    private static Scanner scanner = new Scanner( System.in );

    //Where the coding actually starts :)/>
	public static void main(String [] args)
	{
		String charName= ("");
		boolean checkingName = false;
		

		//Setting up the first variable for yesno!
		//int done = 1;
		do{
				//Prompt the user to choose a character name
        	System.out.print("Choose a name for your character: ");

        // Read a line of text from the user.
            charName = scanner.nextLine();
		
        // Display the input back to the user & ask if they want to change it
            System.out.println("This is your name: " + charName);
            System.out.print("Are you ok with this name? (yes or no):");
            String yesno = scanner.nextLine();
            if (yesno == "yes")
            {
            	checkingName = true;
            } else {
               	checkingName = false;
            }//End of If/Else statement

		}while(!checkingName)
			//--------------------------------------------------------------------------------End of the While statement for the name. Finnally lol. == for comparison, = for assigning

		//Tell them their name and continue asking which town to start in
		System.out.println("Your name is set to " + charName);
		
		//Give instructions, start dialogue, 
		System.out.println("Hello " + charName + "! Please read the rules before starting");
		System.out.println("");
		System.out.println("Instructions:");
		System.out.println("1. Have fun!");
		System.out.println("2. Don't cheat!");
		System.out.println("3. I suck at writing IRL stories so don't hate!");
		System.out.println("");
		System.out.println("STORY START");
		System.out.println("-----------");
		System.out.println("");
		
		//The problem with the story is that to make a program like this you would need mulitple classes and a well made GUI.
		//I'm doing this for my final project but now I have no idea how to link up multiple classes to make different routes as making a Do While statement for
		//Everything would be extremely hard. 
		
	}
}//End of Main Method




Is This A Good Question/Topic? 0
  • +

Replies To: Breaking out of this loop? (Boolean won't work?)

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Breaking out of this loop? (Boolean won't work?)

Posted 12 September 2013 - 08:59 AM

......
You dont comprare Strings with == , if you want to compare Strings use the method equals().
 if (yesno.equals("yes") )
            {
            	checkingName = true;
            } else {
               	checkingName = false;
            }


This post has been edited by ChrisNt: 12 September 2013 - 08:59 AM

Was This Post Helpful? 3
  • +
  • -

#3 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Breaking out of this loop? (Boolean won't work?)

Posted 12 September 2013 - 09:14 AM

Also there is no real need for that if/else block at all. You can put it in one line as .equals returns a boolean:

checkingName = yesno.equals("yes");


And to explain a little more, the == operator will check if the two variables are pointing to the same object in memory (which won't happen in this case) - not that the actual strings themselves have the same value - which is what the .equals() method does.
Was This Post Helpful? 1
  • +
  • -

#4 GoldenFyre   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-September 13

Re: Breaking out of this loop? (Boolean won't work?)

Posted 12 September 2013 - 05:14 PM

Thank you soooo much. Finally understand haha. thanks :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1