14 Replies - 1114 Views - Last Post: 04 February 2013 - 12:18 PM Rate Topic: -----

#1 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

It doesn't print anything

Posted 04 February 2013 - 11:28 AM

Hello, I'm creating a very simple game.
Here's my code so far
import java.util.Scanner;
public class Quest {
public static void main(String[] args){
	System.out.println("You have been summoned to participate in a fighting tournament");
	System.out.println("There are 3 classes available: Warrior, Mage and Archer");
	System.out.print("Choose your class ");
	
	Scanner ui = new Scanner(System.in);
	String input =ui.nextLine();

	if(ui.equals("warrior"))
	{
		
		
		System.out.println("You are a mighty warrior, your inventory consists of: blade, shield and chainmail armour");
		
	}
	
	if(ui.equals("mage"))
	{
		
		System.out.println("You are  a wise mage, your inventory consists of: staff, magic potion and wizard robes");
		
	}
	if(ui.equals("archer"))
	{
		
		System.out.println("You are a cunning wizard, your inventory consists of: bow and arrow, throwing knives and climbing rope");
		
		
		
	}
	
}
}


but when I run it and enter say.. "warrior"
It doesn't print anything, it simply stops working.

Is This A Good Question/Topic? 0
  • +

Replies To: It doesn't print anything

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: It doesn't print anything

Posted 04 February 2013 - 11:31 AM

ui.equals("warrior")


You are checking if the Scanner object equals warrior
You would want to check against the input :)
input.equals("warrior")


Was This Post Helpful? 3
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: It doesn't print anything

Posted 04 February 2013 - 11:31 AM

 if(ui.equals("warrior"))



A Scanner object will never be equal to a String. Try

 if(input.equals("warrior"))

Was This Post Helpful? 2
  • +
  • -

#4 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: It doesn't print anything

Posted 04 February 2013 - 11:32 AM

Thank you
Was This Post Helpful? 0
  • +
  • -

#5 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: It doesn't print anything

Posted 04 February 2013 - 11:47 AM

Another question when I'm faced with an Ork and type "run"
the application terminates, why?
import java.util.Scanner;
public class Quest {
public static void main(String[] args){
	System.out.println("TextQuest V1.0");
	System.out.println("There are 3 classes available: Warrior, Mage and Archer");
	System.out.print("Choose your class ");
	
	Scanner ui = new Scanner(System.in);
	String input =ui.nextLine();

	if(input.equals("warrior"))
	{
		
		
		System.out.println("You are a mighty warrior, your inventory consists of: blade, shield and chainmail armour");
		
	}
	
	if(input.equals("mage"))
	{
		
		System.out.println("You are  a wise mage, your inventory consists of: staff, magic potion and wizard robes");
		
	}
	if(input.equals("archer"))
	{
		
		System.out.println("You are a cunning wizard, your inventory consists of: bow and arrow, throwing knives and climbing rope");
		
		
	}
	System.out.println("You are travelling in the woods and suddenly wild Ork appears!");
	System.out.println("What do you do?");
	System.out.print("  run, fight or bribe? ");
	
	if(input.equals("fight"))
			{
		
		System.out.println("You charge at ork, unfortunately you were not strong enough to defeat him which resulted in your death");
		
			}
	if(input.equals("bribe") || input.equals("run"))
			{
		System.out.println("Congratulations, you managed to stay alive");
		
			}
	
}
}



EDIT: I fixed it with this little bit of code
Scanner ui2 = new Scanner(System.in);
	String input2 = ui.nextLine();

I understood the problem, but when I enter "run" it doesn't do anything here's how that part of the code looks
System.out.println("You are travelling in the woods and suddenly wild Ork appears!");
	System.out.println("What do you do?");
	System.out.print("  run, fight or bribe? ");
	
	Scanner ui2 = new Scanner(System.in);
	String input2 = ui.nextLine();
	if(input2.equals("fight"))
			{
		
		System.out.println("You charge at ork, unfortunately you were not strong enough to defeat him which resulted in your death");
		
			}
	if(input2.equals("bribe") || input.equals("run"))
			{
		System.out.println("Congratulations, you managed to stay alive");
		
			}
	
}
}


Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: It doesn't print anything

Posted 04 February 2013 - 11:49 AM

Well, that is because you are not taking any input from the user.

You will probably want something like this on line 35.

String input = ui.nextLine();


This will enable the user to give the input and accordingly you will get the output.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: It doesn't print anything

Posted 04 February 2013 - 11:49 AM

I don't see where you're getting the input from there. input at that point is still "warrior" or "wizard" or whatever.


EDIT: No need to declare a new Scanner. You drive to the store in your car - when you're done shopping, you don't buy a new car to drive home, do you?

This post has been edited by jon.kiparsky: 04 February 2013 - 11:51 AM

Was This Post Helpful? 1
  • +
  • -

#8 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: It doesn't print anything

Posted 04 February 2013 - 11:51 AM

EDIT: I fixed it with this little bit of code
Scanner ui2 = new Scanner(System.in);
	String input2 = ui.nextLine();

I understood the problem, but when I enter "run" it doesn't do anything here's how that part of the code looks
System.out.println("You are travelling in the woods and suddenly wild Ork appears!");
	System.out.println("What do you do?");
	System.out.print("  run, fight or bribe? ");
	
	Scanner ui2 = new Scanner(System.in);
	String input2 = ui.nextLine();
	if(input2.equals("fight"))
			{
		
		System.out.println("You charge at ork, unfortunately you were not strong enough to defeat him which resulted in your death");
		
			}
	if(input2.equals("bribe") || input.equals("run"))
			{
		System.out.println("Congratulations, you managed to stay alive");
		
			}
	
}
}


Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: It doesn't print anything

Posted 04 February 2013 - 11:52 AM

if(input2.equals("bribe") || input.equals("run"))



input still equals "wizard" or "warrior" or whatever.
Was This Post Helpful? 1
  • +
  • -

#10 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: It doesn't print anything

Posted 04 February 2013 - 11:53 AM

Oh right, Fixed this with
Scanner ui2 = new Scanner(System.in);
06	    String input2 = ui2.nextLine();

Thanks

This post has been edited by am0n: 04 February 2013 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#11 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: It doesn't print anything

Posted 04 February 2013 - 11:54 AM

Well, since you have changed the scanner object to ui2, you need to make the change to ui2 and not ui

String input2 = ui2.nextLine();


regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: It doesn't print anything

Posted 04 February 2013 - 11:59 AM

Stop with the second scanner. You just need one - but if you get a new input, you have to check the right String.

If this is going to turn into a sort of collaborative development of your choose-your-own-adventure, let me just suggest now that one long series of if-this-then-that isn't going to work out at all - combinatorial explosion is going to kill you. You should probably back up and think about design a bit.
Was This Post Helpful? 0
  • +
  • -

#13 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: It doesn't print anything

Posted 04 February 2013 - 12:02 PM

Thanks for your input, John. What would you suggest instead of "ifs" Would switch statements work better?
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: It doesn't print anything

Posted 04 February 2013 - 12:14 PM

Well, ideally, you'd like this to be a loop:
while (game is still going on) {
  present situation
  get user choice
  look up new situation
}


Suppose you consider each situation to be a String, which describes where the user is and what their options are. Store those Strings in a collection of some sort: now all you need to do is map from the user's input to the correct reference.

To make life even easier, you could wrap up the situations in an object, which contains the description String and the lookup table for that String's options.

You could use a switch in that object, I suppose, but it's always better to do lookup with a Collection than with a flow control statement.
Was This Post Helpful? 1
  • +
  • -

#15 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: It doesn't print anything

Posted 04 February 2013 - 12:18 PM

Ah, I see.
Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1