• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to capture white space

 
Joanna Queyquep
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this using a client/server through a browser, or a Java application?

Gareth
 
Joanna Queyquep
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gareth Faulkner:
Is this using a client/server through a browser, or a Java application?

Gareth



Java application
 
Gareth Faulkner
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What input fields are you using or is it straight from the command line?

Gareth
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = ' ';
 
Joanna Queyquep
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much it works
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic