Results 1 to 19 of 19
  1. #1
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default Name titles help

    Hi, I need some help with a java assignment that I just can't seem to get.

    I need to make a program that takes a person's name and then checks if it is one from a list of about 6 others. Those six tell if the person is male or female and appropriately assign the right title to them and print out the name, but if the name they entered isn't the program just prints it out normally. This should continue until someone hits enter when the program asks for their name. Then it should end.

    Example: (Ted is listed, Frank is not)

    Enter name: Ted

    Mr. Ted

    Enter name: Frank

    Frank

    Enter name: (enter)
    (ends)

    Sorry if im not great at explaining, but I really need some help. The part I really need help on is getting it to check the names and assign the title. Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello and welcome to the forum. For starters, please show us the code you've created where you try to solve this problem. This way we can see where it's going wrong.

  3. #3
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    well, to be honest my code is probably complete gibberish at this point. It's also stored on a school computer atm so ill rewrite it in a few minutes to show my progress.
    I have a pretty low self esteem, so thanks for the welcoming. I need some help with java.

  4. #4
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    class titles
    {
    
      public static void main ( String[] args )
      {
        Scanner scan = new Scanner( System.in ); 
    
        String str1; 
        String str2;    
        String str3;    
        String str4;    
        String str5;    
        String str6;
    
    	str1 = new String("Kevin");
    	str2 = new String("Michael"); 
    	str3 = new String("Jake"); 
    	str4 = new String("Cindy"); 
    	str5 = new String("Kate"); 
    	str6 = new String("Amanda");             
    
        System.out.println("What is your name: ");
    	name = scan.nextString();
    
    if(name = str1 || name = str2 || name = str3){
    system.out.println("Mr." + name);
    
    }else{
    
    if(name = str4 || name = str5 || name = str6){
    system.out.println("Mrs." + name);
    
    }else{
    
    if(name =! str1 || name = str2 || name = str3 || name = str4 || name = str5 || name = str6){ 
    system.out.println(+ name);
    
    }else{
    
    if(name == 0){
    System.Exit();
    
    	
      }
    }
    I'm probably completely off with what I have know. I just started java.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Myself, I'd use a collection of Strings such as a HashSet, but if you can't use that, and if you are supposed to use String variables, then I'd suggest a few changes.

    First of all, you are breaking one of the cardinal rules of coding: you are adding more code to bad code. You need to start with the barest minimum of a class, just the class name and curly braces to start with (and the class name should begin with a capital letter), and then you need to compile the java file. If it compiles, then you start adding code, in your case, you'd start by adding the main method, just the method declaration and the method's curly braces, and nothing else. Then make sure that this code compiles and also runs. Nothing will result from the run, but you want to make sure that there are no errors. If you find any errors, then you fix them before moving on to the next step.

    Then you start fleshing out the main method by adding code to it, one or two lines (if for instance you are creating a block such as an if block) at a time. After each line or two, you must compile the code, and if the code doesn't compile, you must fix all errors before adding any more code.

    Keep repeating the steps above -- adding code, compiling, fixing errors, and not adding more code until the errors are fixed.

    Your code above is chock full of errors, too many to list here, and I recommend that you restart using the techniques I've listed above. A few tips though:

    you compare two things with == not with = which is the assignment operator. You don't use either to compare Strings or other objects but rather use the equals(...) method for these. Read up on how to do if and else if statements.

    Best of luck!

  6. #6
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    well thanks for the advice, ill read up on if and else if.
    But is the general concept in any way right? You said you would use HashSet but would if and else if work still?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    if would work, yes, but again, you can't do

    Java Code:
    if (a = b) {
    
    }

    but rather

    Java Code:
    if (a == b) {
    
    }

    and neither with Strings but rather:

    Java Code:
    if (stringA.equals(stringB)) {
    
    }

    Then you'll need to fix your else if's. Something like so:

    Java Code:
    if (stringA.equals(stringB)) {
    
    } else if (stringA.equals(stringC)) {
    
    }

  8. #8
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your help, you cleared up a lot.
    I'm going to start from the top and follow your tips. Wish me luck!

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Kak75 View Post
    Thanks for your help, you cleared up a lot.
    I'm going to start from the top and follow your tips. Wish me luck!
    You got it! Please let us know how things turn out!

  10. #10
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    So far, it's turned out a lot better than it was. It's essentially the same in a way (pretty bulky) so i'm still reading up on a lot, but by taking my time and going step by step and compile by compile, I got some results. The program does what I want it to do but now I need to find out how I can make it repeat until the user hits enter.

    Can someone nudge me in the right direction? I think it has to do with while, but im not 100% sure.

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    you need a do while loop, there are many tutorials available, post again if you are still struggling...

  12. #12
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Well, the problem im having is what to make the condition. Currently I can't even get it to repeat, but I can get it to recognize if they're a Mr. Mrs. or no title. I tried a do while but. again, without knowing what to condition im stumped.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can either use String's equal method:

    Java Code:
    while (!myString.equals("")) {
    
    }

    or use the String isEmpty() method.

    Java Code:
    while (!myString.isEmpty()) {
    
    }

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Your exit condition was the pressing of the enter button, you need to learn how to detect that... try using a KeyListener http://download.oracle.com/javase/tu...ylistener.html
    Last edited by al_Marshy_1981; 12-19-2010 at 04:55 AM.

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    re-reading your post again... Fubarable's solution is the better one, there is no need for key listeners...

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    re-reading your post again... Fubarable's solution is the better one, there is no need for key listeners...
    Yeah, pretty tough to use a KeyListener when you're not coding a GUI. :p

  17. #17
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    give me a break its 5am here :D

  18. #18
    Kak75 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    and it is done! DON DON DOOONNNNN!

    thanks for the help, it worked out just fine. I was pretty lost before, thanks guys. :D

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Kak75 View Post
    and it is done! DON DON DOOONNNNN!

    thanks for the help, it worked out just fine. I was pretty lost before, thanks guys. :D
    Congratulations! Now on to an even bigger and tougher project! :)

Similar Threads

  1. CSV column titles
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-24-2008, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •