Results 1 to 12 of 12
- 10-17-2012, 02:58 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 37
- Rep Power
- 0
Boolean expression to put a name in proper form
I am assigned to make a boolean method that takes a name as a parameter and determines wether or not it is in common form by seeing if there is a comma (standard form is last, first. non standard is first lasy.) . After it determines this, it is supposed to put it into common form if it is not already, by calling on the method that sees wether or not it has a comma. here is my code
package name;
import java.util.Scanner;
/**
*
* @author Zack
*/
public class Name {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner (System.in);
System.out.println("What is the name you want to test? ");
String name = reader.nextLine();
hasComma(name);
}
public static boolean hasComma(String name){
boolean oName;
int x= name.indexOf(',');
if (x == -1) {
oName= false;
}else{
oName= (true);
}
return (oName);
}
public static void convertName() {
}
}
any help would be appreciated, thanks in advance.
- 10-17-2012, 03:02 AM #2
Re: Boolean expression to put a name in proper form
What is your specific question. I see you have posted your requirements and I see you have posted your code. At a quick glance the code seems to do what you want. So what is your issue?
BTW your hasComma method can be reduced to a single line of code.
- 10-17-2012, 03:07 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 37
- Rep Power
- 0
Re: Boolean expression to put a name in proper form
Well right now, after I type in the name, it does not return anything, it just says build stopped and I have no Idea what to do for the convertname method.
- 10-17-2012, 03:10 AM #4
- 10-17-2012, 03:19 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 37
- Rep Power
- 0
Re: Boolean expression to put a name in proper form
How do I make it print the result?
- 10-17-2012, 03:23 AM #6
Re: Boolean expression to put a name in proper form
How do you make a program print anything? Use a print statement! You can either wrap the print statement around the call to the hasComma method. Or you can assign the return value to a variable and then print the variable. If you do not know/understand either of those options then I suggest reading your text book or searching the web as there will be numerous examples of both.
- 10-17-2012, 05:30 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 37
- Rep Power
- 0
Re: Boolean expression to put a name in proper form
Thank you so much for the help! you saved me. I only have one more thing to do on this code. I have to make it loop in the main method, asking for a name, until a blank is entered, what change would I have to make in main to do this? Thanks again for all of your help.
public class Name {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner (System.in);
System.out.println("What is the name you want to test? ");
String name = reader.nextLine();
convertName(name);
}
public static boolean hasComma(String name){
boolean oName;
int x= name.indexOf(',');
if (x == -1) {
oName= false;
}else{
oName= (true);
}
return (oName);
}
public static boolean convertName(String name) {
boolean convert = false;
hasComma(name);
if (hasComma(name)== true) {
convert= true;
}else{
String s = name;
String[] arr = s.split(" ");
System.out.println (arr[1]+ ","+ arr[0]);
}
return (convert);
}
}
- 10-17-2012, 06:21 AM #8
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: Boolean expression to put a name in proper form
- 10-17-2012, 10:57 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Boolean expression to put a name in proper form
Java Code:return name.indexOf(',') > -1;
Many of us won't bother reading unformatted code.Please do not ask for code as refusal often offends.
** This space for rent **
- 10-19-2012, 03:37 AM #10
Re: Boolean expression to put a name in proper form
Oh the irony!
Tolls supplied the code despite his signature. But yes, that is exactly how I would have coded it.
- 10-19-2012, 11:00 AM #11
Member
- Join Date
- Sep 2012
- Location
- Guntur, India
- Posts
- 27
- Rep Power
- 0
Re: Boolean expression to put a name in proper form
You can simply use.
if(name.contains(",")) return true;
else return false;
[Moderator edit: link removed]Last edited by DarrylBurke; 10-22-2012 at 05:07 AM.
- 10-19-2012, 11:16 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Boolean expression to put a name in proper form
Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
Logic Expression ... possible bug for short circuit boolean operator?
By JimmyD in forum Advanced JavaReplies: 6Last Post: 10-27-2011, 07:40 PM -
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 01:01 AM -
Looking for pseudocode to convert Prefix Expression & Infix Expression to expression
By dragstang86 in forum New To JavaReplies: 2Last Post: 07-18-2011, 08:11 AM -
[SOLVED] Boolean Expression Evaluation Framework
By priyanka.dandekar in forum Advanced JavaReplies: 8Last Post: 03-27-2010, 03:35 PM -
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07-11-2007, 05:11 AM
Bookmarks