Jump to content

Text-Based Adventure - Problems With A Method

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Devilb0y

Devilb0y

    Newbie

  • Members
  • PipPip
  • 10 posts
'Lo all,

So since I last posted I've been doing my best to broaden my Java horizons. For those who weren't kind enough to respond to my first question here: I've been programming about a week now and Java is my first language. With that in mind I hope this doesn't sound like a silly question.

In the interest of consolidating what I've learned in the last week I'm turning my hand to a really simple text adventure game as a means of experimenting with variables, conditions, loops and input. Thus far all I've designed is a start 'screen' and the output I want once the user types 'start'. However I seem to be having some problems in achieving this. Maybe if I post the code it'll make it clearer:

import java.util.Scanner;


public class TextAdventure {



	public static void main(String[] args) {

		// Display 'Dragon Quest IV: The Hunt For Red October' and advise user of input to start game. When user types start this should invoke method 'Adventure'

		System.out.println("DUNGEON QUEST IV:");

		System.out.println();

		System.out.println("The Hunt For Red October");

		System.out.println();

		System.out.println("Please type 'start' to begin your adventure");

		Scanner scan = new Scanner (System.in);

		String start = scan.nextLine();

		if (start =="start") {

			adventure();	


		}

	}

	

	public static void adventure()	{

		// Screen 1. Description and options.

		System.out.println("You wake from a largley sleepless night in a dark room. Through the haze you can make out a table beside you, a door across from you and a wardrobe in the corner.")

		System.out.println();

		System.out.println("Will you:")

		System.out.println();

		System.out.println("a) Examine the table");

		System.out.println("b) Look at the wardrobe");

		System.out.println("c) Try the door");

	}

}


So at the end of the main method I'm trying to get my programme to invoke the method adventure() if the user types start. However when I run it and type 'start' I get no response whatsoever. I'm assuming there's another way to tell your programme to expect a certain kind of input that I'm not aware of. Either way: could someone tell me what I'm doing wrong?

Thanks,

Dev.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Don't use == for testing String equivalence. That tries to test reference equality, which returns false because start and "start" are not the same object reference.

Instead, use String.equals() for testing String value equivalence.

Also, keep in mind, Scanner.nextLine() also returns whitespace surrounding your string, so you might want to consider using String.trim() to snip off the leading and trailing whitespace before comparing the scanned token. Also, consider String.equalsIgnoreCase() if you want to make your comparison case insensitive.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Devilb0y

Devilb0y

    Newbie

  • Members
  • PipPip
  • 10 posts
So just to be clear, should I drop the entire if conditional and use String.equals(start) instead?

Also, I haven't actually learned String classes yet (excuse the potentially embarrassing lack of technical know-how). Would I need to import a new class in order to use that syntax?

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Devilb0y said:

So just to be clear, should I drop the entire if conditional and use String.equals(start) instead?

Also, I haven't actually learned String classes yet (excuse the potentially embarrassing lack of technical know-how). Would I need to import a new class in order to use that syntax?

Nope, you don't need to import anything to use the String class.

Sample:

String s = "abc";

String t = "def";

System.out.println( s.equals(t) );

if( s.equals(t) ) 

    System.out.println(s + " does not equal " + t );






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users