Need String help!

I wanted to learn Java, so I bought "Java for Dummies 4th Edition". I think it's a little outdated, but seems fine. I'm creating a text-based game where the user inputs a question, and then that text is put into a string. The problem is, the string won't accept spaces, and only takes the first word. I was thinking about having the questions put into a .txt file, and then read, but that seems to much work for a small problem I'm sure can be worked out. So, what do I do? Thanks!

[492 byte] By [InsaneMonkeya] at [2007-11-27 11:07:16]
# 1

Strings accept spaces and more than just the first word all the time. So basically (since you showed none of your work which produced the problem) all we can say at this point is "You've done something wrong".

warnerjaa at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 2

Let me guess, you're taking input as a command line parameter? Then looking at args[0] for the question? If so, then put the input in "" on the command line.

java MyProgram "This is my question?"

hunter9000a at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 3

Ok I'm creating a "What If..." game. The code is still VERY unfinished and sloppy...I guess.

import static java.lang.System.in;

import static java.lang.System.out;

class Info {

String player1;

String player2;

String player3;

String player4;

String player5;

String player6;

int ccoc;

int players;

String player1question;

String player2question;

String player3question;

String player4question;

String player5question;

String player6question;

String player1answer;

String player2answer;

String player3answer;

String player4answer;

String player5answer;

String player6answer;

void display() {

if (players == 6) {

out.println("These are the players you entered: ");

out.print(player1);

out.print(", ");

out.print(player2);

out.print(", ");

out.print(player3);

out.print(", ");

out.print(player4);

out.print(", ");

out.print(player5);

out.print(", ");

out.print(player6);

out.println();

out.println("__");

} else if (players == 5) {

out.println("These are the players you entered: ");

out.print(player1);

out.print(", ");

out.print(player2);

out.print(", ");

out.print(player3);

out.print(", ");

out.print(player4);

out.print(", ");

out.print(player5);

out.println();

out.println("__");

} else if (players == 4) {

out.println("These are the players you entered: ");

out.print(player1);

out.print(", ");

out.print(player2);

out.print(", ");

out.print(player3);

out.print(", ");

out.print(player4);

out.println();

out.println("__");;

} else if (players == 3) {

out.println("These are the players you entered: ");

out.print(player1);

out.print(", ");

out.print(player2);

out.print(", ");

out.print(player3);

out.println();

out.println("__");

} else if (players == 2) {

out.println("These are the players you entered: ");

out.print(player1);

out.print(", ");

out.print(player2);

out.println();

out.println("__");

}

}

}

import static java.lang.System.in;

import static java.lang.System.out;

import java.util.Scanner;

import java.util.Random;

class WhatIf {

public static void main (String args[]) {

Info Whatif = new Info();

Scanner myScanner = new Scanner(System.in);

out.print("How many players? (Max: 6): ");

Whatif.players = myScanner.nextInt();

int playercheck = 0;

while (playercheck != 1)

if (Whatif.players < 7 && Whatif.players > 1) {

out.println("Players: Ok");

out.println("__");

playercheck++;

} else {

if (playercheck == 1) {

out.println("*************************************************************");

out.println("*Enter a number NO BIGGER than SIX(6) OR SMALLER than TWO(2)*");

out.println("*************************************************************");

} else {

out.println("*************************************************************");

out.println("*Enter a number NO BIGGER than SIX(6) OR SMALLER than TWO(2)*");

out.println("*************************************************************");

out.print("How many players? (Max: 6): ");

Whatif.players = myScanner.nextInt();

}

}

out.print("Counter-clockwise(1) or clockwise(2)? (Type one of the numbers): ");

Whatif.ccoc = myScanner.nextInt();

int ccoccheck = 0;

while (ccoccheck != 1)

if (Whatif.ccoc == 1) {

out.println("Counter-clockwise or clockwise: Ok");

out.println("");

ccoccheck = 1;

} else {

if (Whatif.ccoc == 2) {

out.println("Counter-clockwise or clockwise: Ok");

out.println("");

ccoccheck = 1;

} else {

out.println("*********************************************************");

out.println("*Enter either 1 for counter-clockwise or 2 for clockwise*");

out.println("*********************************************************");

out.print("Counter-clockwise(1) or clockwise(2)? (Type one of the numbers): ");

Whatif.ccoc = myScanner.nextInt();

}

}

out.println("Enter the player's names: ");

if (Whatif.players == 6) {

out.print("Player 1: ");

Whatif.player1 = myScanner.next();

out.print("Player 2: ");

Whatif.player2 = myScanner.next();

out.print("Player 3: ");

Whatif.player3 = myScanner.next();

out.print("Player 4: ");

Whatif.player4 = myScanner.next();

out.print("Player 5: ");

Whatif.player5 = myScanner.next();

out.print("Player 6: ");

Whatif.player6 = myScanner.next();

Whatif.display();

} else if (Whatif.players == 5) {

out.print("Player 1: ");

Whatif.player1 = myScanner.next();

out.print("Player 2: ");

Whatif.player2 = myScanner.next();

out.print("Player 3: ");

Whatif.player3 = myScanner.next();

out.print("Player 4: ");

Whatif.player4 = myScanner.next();

out.print("Player 5: ");

Whatif.player5 = myScanner.next();

Whatif.display();

} else if (Whatif.players == 4) {

out.print("Player 1: ");

Whatif.player1 = myScanner.next();

out.print("Player 2: ");

Whatif.player2 = myScanner.next();

out.print("Player 3: ");

Whatif.player3 = myScanner.next();

out.print("Player 4: ");

Whatif.player4 = myScanner.next();

Whatif.display();

} else if (Whatif.players == 3) {

out.print("Player 1: ");

Whatif.player1 = myScanner.next();

out.print("Player 2: ");

Whatif.player2 = myScanner.next();

out.print("Player 3: ");

Whatif.player3 = myScanner.next();

Whatif.display();

} else if (Whatif.players == 2) {

out.print("Player 1: ");

Whatif.player1 = myScanner.next();

out.print("Player 2: ");

Whatif.player2 = myScanner.next();

Whatif.display();

}

// int RandomNumber = new Random().nextInt(10) + 1;

//for (int count = 0; count = Whatif.players; count++) {

if (Whatif.players == 6) {

out.print(Whatif.player1);

out.print(", enter your question: ");

Whatif.player1question = myScanner.next();

out.println("Player 1 question: Ok");

out.print(Whatif.player2);

out.print(", enter your question: ");

Whatif.player2question = myScanner.next();

out.println("Player 2 question: Ok");

out.print(Whatif.player3);

out.print(", enter your question: ");

Whatif.player3question = myScanner.next();

out.println("Player 3 question: Ok");

out.print(Whatif.player4);

out.print(", enter your question: ");

Whatif.player4question = myScanner.next();

out.print("Player 4 question: Ok");

out.print(Whatif.player5);

out.println(", enter your question: ");

Whatif.player5question = myScanner.next();

out.println("Player 5 question: Ok");

out.print(Whatif.player6);

out.println(", enter your question: ");

Whatif.player6question = myScanner.next();

out.println("Player 6 question: Ok");

} else if (Whatif.players == 5) {

out.print(Whatif.player1);

out.println(", enter your question: ");

Whatif.player1question = myScanner.next();

out.println("Player 1 question: Ok");

out.print(Whatif.player2);

out.println(", enter your question: ");

Whatif.player2question = myScanner.next();

out.println("Player 2 question: Ok");

out.print(Whatif.player3);

out.println(", enter your question: ");

Whatif.player3question = myScanner.next();

out.println("Player 4 question: Ok");

out.print(Whatif.player4);

out.println(", enter your question: ");

Whatif.player4question = myScanner.next();

out.println("Player 4 question: Ok");

out.print(Whatif.player5);

out.println(", enter your question: ");

Whatif.player5question = myScanner.next();

out.println("Player 5 question: Ok");

} else if (Whatif.players == 4) {

out.print(Whatif.player1);

out.println(", enter your question: ");

Whatif.player1question = myScanner.next();

out.println("Player 1 question: Ok");

out.print(Whatif.player2);

out.println(", enter your question: ");

Whatif.player2question = myScanner.next();

out.println("Player 2 question: Ok");

out.print(Whatif.player3);

out.println(", enter your question: ");

Whatif.player3question = myScanner.next();

out.println("Player 3 question: Ok");

out.print(Whatif.player4);

out.println(", enter your question: ");

Whatif.player4question = myScanner.next();

out.println("Player 4 question: Ok");

} else if (Whatif.players == 3) {

out.print(Whatif.player1);

out.println(", enter your question: ");

Whatif.player1question = myScanner.next();

out.println("Player 1 question: Ok");

out.print(Whatif.player2);

out.println(", enter your question: ");

Whatif.player2question = myScanner.next();

out.println("Player 2 question: Ok");

out.print(Whatif.player3);

out.println(", enter your question: ");

Whatif.player3question = myScanner.next();

out.println("Player 3 question: Ok");

} else if (Whatif.players == 2) {

out.print(Whatif.player1);

out.print(", enter your question: ");

Whatif.player1question = myScanner.next();

out.println("Player 1 question: Ok");

out.print(Whatif.player2);

out.print(", enter your question: ");

Whatif.player2question = myScanner.next();

out.println("Player 2 question: Ok");

}

}

}

The problem is the Whatif.player1question part and so on and so forth down the if statement. I tried using a loop insted, but it was giving me trouble, so I used this sloppy if statement. Feel free to critisize and whatnot. Thanks!

InsaneMonkeya at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 4

edit -- noop

Hippolytea at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 5

oooOOOooo weee t-shane...

TuringPesta at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 6

> I tried using a loop insted, but it was giving me trouble

Try to work on resolving that trouble. It will be well worth it to use a loop here.

~

yawmarka at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 7

So how to I fix my String problem?

InsaneMonkeya at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 8

You could get rid of 80% of your typing if you used methods.

Here is an example. Instead of typing each case of number of

players use a method:

// TOTAL INSANITY

out.println("These are the players you entered: ");

out.print(player1);

out.print(", ");

out.print(player2);

out.print(", ");

out.print(player3);

out.print(", ");

out.print(player4);

out.print(", ");

out.print(player5);

out.print(", ");

out.print(player6);

out.println();

// The "right" way

String[] players = new String[]{ "bob", "charlie", "mary" };

public void doPlayerPrompt(String[] players){

out.println("These are the players you entered: ");

for(int i = 0; i < players.length; i++){

out.print(players[ i ]);

out.print(", ");

}

out.println();

}

TuringPesta at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 9

> So how to I fix my String problem?

Compare the next() and nextLine() methods in the Scanner api.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

hunter9000a at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 10

Insane Monkey here is a rewrite of your program.

Hopefully you can pick up a few things.

Youll notice its a lot less code and much easier.

import java.util.Scanner;

public class WhatIfGame{

public static void main(String[] args){

new WhatIfGame();

}

public WhatIfGame(){

try{

scanner = new Scanner(System.in);

System.out.print("Number of Players: ");

int numPlayers = scanner.nextInt();

scanner.nextLine();

players = new Player[numPlayers];

getPlayerNames();

getPlayerQuestions();

} catch(Exception e){

e.printStackTrace();

}

}

public void getPlayerNames() throws Exception{

System.out.println();

for(int i = 0; i < players.length; i++){

System.out.print("Player #" + (i+1) + "'s Name: ");

String name = scanner.nextLine();

players[i] = new Player();

players[i].name = name;

}

}

public void getPlayerQuestions() throws Exception{

System.out.println();

for(int i = 0; i < players.length; i++){

System.out.print(players[i].name.toUpperCase() + "'s Question: ");

String question = scanner.nextLine();

players[i].question = question;

}

}

public class Player{

String name;

String question;

}

Player[] players;

Scanner scanner;

}

TuringPesta at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 11

I don't know what any of that i, throw, or catch stuff. I'm not that far in the book yet. Should I finish the book then try to finish my program first?

InsaneMonkeya at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 12

Just delete the try / catch lines and the "throws Eception" text.

All the for loop is doing is counting from 0 to (array size - 1).

"i" stores the number.

Arrays are indexed from 0;

players[0] = jim

players[1] = bob

players[2] = george

for(int i = 0; i < players.length; i++){

System.out.println(players[ i ])

}

Will just print "jim bob george".

Easy huh?

You should be programming constantly.

You should be programming more than reading.

You should be programming in a solid object oriented way as soon as possible.

TuringPesta at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 13

The book hasn't talked about Arrays yet. I just copied a program with the IOException thing. The program doesn't even work, I copied just like it said. >_> Oh well. I will finish the game once I'm done with the book.

InsaneMonkeya at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 14

Dont get discouraged.

For loops are the life blood of programming so you should

learn them as soon as possible. Basically they are just a way

of counting from one number to another.

The best thing about technical books is that they dont have to

be read in order. Just skip to the chapter on for loops and then

go back to where you were.

TuringPesta at 2007-7-29 13:21:01 > top of Java-index,Java Essentials,New To Java...
# 15

The book keeps talking about "methods". I know I've read over them, but I'm kind of hazy on what they are. Can someone explain them to me? Thanks!

InsaneMonkeya at 2007-7-29 13:21:07 > top of Java-index,Java Essentials,New To Java...
# 16

A method is a function that you call to do something.

It can take 0 or more parameters and can return nothing (void)

or ONLY ONE Object or primitive.

In most starter books methods are called the "doers" because they do something.

The methods in the code I gave you are

getPlayerNames()

getPlayerQuestions()

Neither method takes any arguments and they return nothing (void).

This method takes 2 arguments and returns 1 primitive:

public int add(int firstNumber, int secondNumber){

return firstNumber + secondNumber;

}

TuringPesta at 2007-7-29 13:21:07 > top of Java-index,Java Essentials,New To Java...
# 17

> The book keeps talking about "methods". I know I've

> read over them, but I'm kind of hazy on what they

> are. Can someone explain them to me? Thanks!

Now that would be insane indeed, Mr. Monkey. If you can't get it through the book(s) you have, what hope would any of us have of explaining it any better to you?

warnerjaa at 2007-7-29 13:21:07 > top of Java-index,Java Essentials,New To Java...
# 18

> Insane Monkey here is a rewrite of your program.

> Hopefully you can pick up a few things.

> Youll notice its a lot less code and much easier.

Ahhh, Groovy. :o)

#! /usr/bin/groovy

players = []

console = new Scanner(System.in)

print "Enter the number of players: "

playerCount = Integer.parseInt(console.nextLine())

1.upto(playerCount) { number ->

print "Player $number's name: "

name = console.nextLine()

players += new Player(id: number, name: name)

}

players.each { player ->

print "$player.id) $player.name's question: "

player.question = console.nextLine()

}

class Player {

String id, name, question

}

~

yawmarka at 2007-7-29 13:21:07 > top of Java-index,Java Essentials,New To Java...
# 19

you cant pick ac/dc songs but you sure as hell can write code, lol

TuringPesta at 2007-7-29 13:21:07 > top of Java-index,Java Essentials,New To Java...
# 20

that code is also functionally and aesthetically gorgeous, by the way, lol.

it makes me wonder what the hell javafx is trying to accomplish?

i think i should revisit groovy.

TuringPesta at 2007-7-29 13:21:07 > top of Java-index,Java Essentials,New To Java...