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
posted
0
Is this using a client/server through a browser, or a Java application?
Gareth
Joanna Queyquep
Greenhorn
Joined: Oct 04, 2005
Posts: 4
posted
0
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
posted
0
What input fields are you using or is it straight from the command line?
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:
//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)"; }
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
posted
0
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.