Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default Easy program...need help (urgent)

    My program is supposed to ask a user for their first and last name. And, then return results.

    One result is the first and last initials are the same, then it would return the user's name in uppercase.

    And, if their initials are different then it would return the user's name in lowercase.

    I have to use these methods:
    Java Code:
    toUpperCase()
    Java Code:
    toLowerCase()
    I am a Java n00b.

  2. #2
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Please can somebody help me. I am lost.
    I am a Java n00b.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import acm.graphics.*;
    import acm.program.GraphicsProgram;
    import java.awt.Color;
    import java.awt.event.*;
    import MyStaticMethods.Prog1;
    import java.util.*;
    import java.text.DecimalFormat;
    
    public class MrInitials {
    
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            {
    			String result;
    
            	System.out.print(" enter  your first initial ");
                int f = scanner.nextInt();
                System.out.print(" enter last initial ");
                int l = scanner.nextInt();
                System.out.print(" enter your whole first name ");
                String first = scanner.nextLine();
                System.out.print(" enter your whole last name ");
                String last = scanner.nextLine();
                System.out.print(" Are you first and last initials the same? ");
                String answer = scanner.nextInt();
    
             	if(answer = "yes")
                {
    				first.toUpperCase();
    				last.toUpperCase();
    			}
    			else
    			{
    				first.toLowerCase();
    				last.toLowerCase();
    			}
    			result = "Your name is " + first +  last;
     			System.out.println(result);
    
    
           	}
        }
    
    
    }
    I keep getting this error

    Java Code:
    H:\Java Programs\Final Review\MrInitials.java:29: incompatible types
    found   : java.lang.String
    required: boolean
             	if(answer = "yes")
                              ^
    1 error
    I am a Java n00b.

  5. #5
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    I am not even doing it right. I am taking the easy way. If you would can you modify my code for it to find the initials instead of me asking if they are the same?
    I am a Java n00b.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Why not instead of having the user input their initials just take the first character of each string and compare them with the .equals. Remember the first character is in the 0 position.
    My IP address is 127.0.0.1

  8. #8
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Quote Originally Posted by Zosden View Post
    Why not instead of having the user input their initials just take the first character of each string and compare them with the .equals. Remember the first character is in the 0 position.
    Thanks both of you. What code would I use for this. Like, how to I implement this? This method is way better than asking the user if their initials are the same. That is kind of cheating, lol. Thanks to anyone that can help.
    I am a Java n00b.

  9. #9
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default

    To get the first character of the string use the substring() method of the String class. Check the API:
    String (Java 2 Platform SE 5.0)

  10. #10
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Sorry, I still don't understand where to put it. Sorry, I have only been programming a couple months.
    I am a Java n00b.

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    do if(firstname.substring(0).equals(lastname.substrin g(0)).
    My IP address is 127.0.0.1

  12. #12
    gogoc is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    u can use first.equalsIgnoreCase(last)
    and then convert first an last accordingly
    get new coding problems at CODERWORDS

  13. #13
    gogoc is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Zebra View Post
    Java Code:
    import java.util.Scanner;
    import acm.graphics.*;
    import acm.program.GraphicsProgram;
    import java.awt.Color;
    import java.awt.event.*;
    import MyStaticMethods.Prog1;
    import java.util.*;
    import java.text.DecimalFormat;
    
    public class MrInitials {
    
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            {
    			String result;
    
            	System.out.print(" enter  your first initial ");
                int f = scanner.nextInt();
                System.out.print(" enter last initial ");
                int l = scanner.nextInt();
                System.out.print(" enter your whole first name ");
                String first = scanner.nextLine();
                System.out.print(" enter your whole last name ");
                String last = scanner.nextLine();
                System.out.print(" Are you first and last initials the same? ");
                String answer = scanner.nextInt();
    
             	if(answer = "yes")
                {
    				first.toUpperCase();
    				last.toUpperCase();
    			}
    			else
    			{
    				first.toLowerCase();
    				last.toLowerCase();
    			}
    			result = "Your name is " + first +  last;
     			System.out.println(result);
    
    
           	}
        }
    
    
    }
    I keep getting this error

    Java Code:
    H:\Java Programs\Final Review\MrInitials.java:29: incompatible types
    found   : java.lang.String
    required: boolean
             	if(answer = "yes")
                              ^
    1 error
    here u have used assignment
    u should use comparison == so that a boolean is returned
    get new coding problems at CODERWORDS

  14. #14
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Thanks guys. I will try this and post results. I appreciate the help.
    I am a Java n00b.

  15. #15
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    I can't get it to work all the way, I ran out of time anyway for this project. Thanks for the help guys. I will still get a good grade for making this far. Thanks.

    And yes, my teacher lets us use anything on the internet (including forums, so this isn't cheating, lol).

    Here is as far as I got:
    Java Code:
    import java.util.Scanner;
    import acm.graphics.*;
    import acm.program.GraphicsProgram;
    import java.awt.Color;
    import java.awt.event.*;
    import MyStaticMethods.Prog1;
    import java.util.*;
    import java.text.DecimalFormat;
    
    public class MrInitials {
    
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
    			String result;
    
                System.out.print(" enter your first name ");
                String first = scanner.nextLine();
                System.out.print(" enter your last name ");
                String last = scanner.nextLine();
    
    
             	if(first.substring(0).equals(last.substring(0)))
                {
    				first.toUpperCase();
    				last.toUpperCase();
    				result = "Your name is " + first + last;
    
    			}
    			else
    			{
    				first.toLowerCase();
    				last.toLowerCase();
    				result = "Your name is " + first + last;
    
    			}
    
    		System.out.println(result);
        }
    }
    I am a Java n00b.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by gogoc View Post
    here u have used assignment
    u should use comparison == so that a boolean is returned
    Not a good choice though.

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    @Zebra

    Here is the error you have made.

    Java Code:
    if(first.substring(0).equals(last.substring(0)))
    Passing argument in wrong way. If you give one argument at the substring() it is the beginning index. So sub string derived is up to the end of the string.

    Java Code:
    import java.util.Scanner;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class StringTest {
    
        /**
         * @param args the command line arguments
         */
       
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner ss = new Scanner(System.in);
            
            System.out.println("String one: ");
            String str1 = ss.next();
            
            System.out.println("String two");
            String str2 = ss.next();
            
            if(str1.substring(0, 0).equals(str2.substring(0, 0))) {
                System.out.println("Your name is : " + str1.toUpperCase() +
                        " " + str2.toUpperCase());
            }
            else {
                System.out.println("Your name is : " + str1.toLowerCase() +
                        " " + str2.toLowerCase());            
            }
        }
    
    }

  19. #19
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    just wondering why did you import all those classes?
    You just got T4NK3D!
    Adonrcicg to rcesraeh the haumn mnid is clbapae of rdanieg tihs. -Jvaa Sun Moricyssemts-

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Page 1 of 2 12 LastLast

Similar Threads

  1. urgent:calling c program from java
    By rach in forum New To Java
    Replies: 6
    Last Post: 01-19-2010, 12:37 PM
  2. Noob question- easy
    By mattonitto in forum New To Java
    Replies: 7
    Last Post: 06-13-2008, 01:26 AM
  3. What does this mean (Very Easy)
    By Zebra in forum New To Java
    Replies: 6
    Last Post: 05-01-2008, 02:46 PM
  4. help me with a realy easy program (substring)
    By michcio in forum New To Java
    Replies: 7
    Last Post: 01-27-2008, 01:41 AM
  5. Easy question
    By JavaNoob in forum New To Java
    Replies: 10
    Last Post: 08-03-2007, 11:28 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
  •