wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to capture white space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to capture white space" Watch "how to capture white space" New topic
Author

how to capture white space

Joanna Queyquep
Greenhorn

Joined: Oct 04, 2005
Posts: 4
hello, i'm doing a voting program i'm almost done but i cant do this last phase:

on registration, in which the user enters the names of each candidate (if the user enters ' ' - a single space - when the name of the candidate i is requested, the name should default to "Candidate i") - e.g. "Candidate 3" for the third candidate).

How exactly do i capture a white space and put it in a variable? I've tried:
//null
[:space]
[:blank:]
but none of them works

Please help

Thanks heaps in advance!
Gareth Faulkner
Greenhorn

Joined: Oct 29, 2004
Posts: 22
Is this using a client/server through a browser, or a Java application?

Gareth
Joanna Queyquep
Greenhorn

Joined: Oct 04, 2005
Posts: 4
Originally posted by Gareth Faulkner:
Is this using a client/server through a browser, or a Java application?

Gareth



Java application
Gareth Faulkner
Greenhorn

Joined: Oct 29, 2004
Posts: 22
What input fields are you using or is it straight from the command line?

Gareth
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14423
    
  23

Is it a GUI application? Or are you reading the name from the console window? Post some code instead of keeping us guessing.

To directly answer your question ("How exactly do i capture a white space and put it in a variable?"), but this is probably not what you're looking for:

String space = " ";

or

char space = ' ';


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joanna Queyquep
Greenhorn

Joined: Oct 04, 2005
Posts: 4
//gets the number of candidates
System.out.print("How many candidates (1-8)? ");
strTemp = k.readString();
intCan = Integer.parseInt(strTemp);
bot = new AECBot(intCan); //initialize number of candidates
winner = intCan;

//set tracing off
bot.setTracing(false);


//gets the names of each candidate
cnt = 0;
for (i = 0; i < (bot.getNumCandidates()); i++)
{
//asks for name of candidate
System.out.print("\nPlease type in the name of the next candidate: ");
System.out.print("(defaults to 'Candidate " + (i+1) + "'): ");
strTemp = k.readString();

//if name entered is space
if (strTemp == " ") //null)//"[:space:]"//"[:blank:]") -----------> none of these work
{
strTemp = "Candidate " + "Integer.toString(i+1)";
}


bot.setCandidateName(i, strTemp);
}

***************************************************************

Sorry I didn't include the code :-s

What it does is to ask the user for the candidate's name. if the provided name is a single space, i want to store the default name, otherwise i'll store the provided name

Default name: Candidate i, where i is the number of the candidate

Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
Joanna, posting your code makes all the difference.

You need to use String.equals(), which compares the contents of the strings. The == operator just tests the String references for object identity.
Joanna Queyquep
Greenhorn

Joined: Oct 04, 2005
Posts: 4
thank you so much it works
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to capture white space