Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: String Manipulation help!!!!!!!

  1. #1
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Unhappy String Manipulation help!!!!!!!

    I am creating a java program and have been working on it for days and cannot figure out how to correct the error.
    here is the program:

    /*
    // A program that aasks the user to enter the name of his or her favorite city.
    // Designed by Aisha Harris
    */
    public class StringManipulator3

    { // begin class

    public static void main(String[] args)

    { // begin main method

    int stringSize;
    String name; //To hold the city's name
    stringSize = name.length ();

    String message = ("What is the name of your favorite city? ");
    String upper = name.toUpperCase ();
    String lower = name.toLowerCase ();
    char letter = name.charAt (0);

    System.out.println (message);
    System.out.println(stringSize);
    System.out.println(upper);
    System.out.println(lower);
    System.out.println(letter);

    } // end main method

    } // end class

    I keep getting the cannot find symbol error message. Can someone please help!!!!!!!!!!!!!!!!!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: String Manipulation help!!!!!!!

    initialize the string name!

    String name; //To hold the city's name

    -->
    String name = "cityname"

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: String Manipulation help!!!!!!!

    I keep getting the cannot find symbol error message.
    Please copy and paste here the full text of the error message.

    The compiler's message means you are using a symbol that it can not find a definition for. Look at the symbol in the error message and make sure it has a definition.

    Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    Now it is telling me that variable name is already defined.


    Quote Originally Posted by eRaaaa View Post
    initialize the string name!

    String name; //To hold the city's name

    -->
    String name = "cityname"

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

    Default Re: String Manipulation help!!!!!!!

    And as usual the error message is correct. Do not re-declare the same variable. Instead simply use it.

  6. #6
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    StringManipulator3.java:15: error: variable name might not have been initialized

    stringSize = name.length ();

  7. #7
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    Thank you all for replying so quickly.

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

    Default Re: String Manipulation help!!!!!!!

    Quote Originally Posted by Sunshine2012 View Post
    StringManipulator3.java:15: error: variable name might not have been initialized

    stringSize = name.length ();

    Have you solved this then? Are you checking the name String's size before or after you put a valid String into it?

  9. #9
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    No it has not been resolved. I can't even get to that point. It won't run with error.

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

    Default Re: String Manipulation help!!!!!!!

    Quote Originally Posted by Sunshine2012 View Post
    No it has not been resolved. I can't even get to that point. It won't run with error.
    OK, then have you taken my suggestion to heart? Looking at your code, are you calling the length() method on name before or after it has been assigned a valid String?

  11. #11
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    I have only done what you see. I am just starting so this is it.

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

    Default Re: String Manipulation help!!!!!!!

    Quote Originally Posted by Sunshine2012 View Post
    I have only done what you see. I am just starting so this is it.
    OK, then again, try to see where in your code you actually assign a String to the name variable. Don't try to call the length method until after this is done. The solution should thus be obvious.

  13. #13
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    You need to initialize String name;

    You get an error because on the next line you declare a variable stringSize = name.length();

    BUT YOU HAVEN'T INITIALIZED THE 1ST VARIABLE - String name;

    You need to change it to

    Java Code:
    String name = "Put any city you want here";

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

    Default Re: String Manipulation help!!!!!!!

    Quote Originally Posted by lannie1980 View Post
    You need to initialize String name;

    You get an error because on the next line you declare a variable stringSize = name.length();

    BUT YOU HAVEN'T INITIALIZED THE 1ST VARIABLE - String name;

    You need to change it to

    Java Code:
    String name = "Put any city you want here";
    This solution will solve his error, but will give him the wrong length for the City entered. Please let's see if we can nudge the original poster to find the correct solution.

  15. #15
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    Thank you all for your help. I have figured it out.

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

    Default Re: String Manipulation help!!!!!!!

    Quote Originally Posted by Sunshine2012 View Post
    Thank you all for your help. I have figured it out.
    Great! So what is your solution?

  17. #17
    Amok is offline Member
    Join Date
    Sep 2012
    Location
    Australia
    Posts
    12
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    Just do this

    String name = "" ;

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

    Default Re: String Manipulation help!!!!!!!

    Quote Originally Posted by Amok View Post
    Just do this

    String name = "" ;
    If this is all you've changed, then you could be in trouble as this may result in your finding the length of the Capital String as 0, and this is incorrect. A better solution is to move where you call the length() method to after you've assigned a String to name.

  19. #19
    Sunshine2012 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: String Manipulation help!!!!!!!

    Sorry it took me so long to reply. I just got back on. Here is what I did:

    import java.util.Scanner; //Needed for the Scanner class

    /*
    // A program that asks the user to enter the name of his or her favorite city.
    // Designed by Aisha Harris
    */
    public class StringManipulator

    { // begin class

    public static void main(String[] args)


    { // begin main method

    String name; //To hold the city's name

    Scanner keyboard = new Scanner(System.in);

    System.out.print("What is the name of your favorite city? ");
    name = keyboard.nextLine();

    String upper = name.toUpperCase ();
    String lower = name.toLowerCase ();
    char letter = name.charAt (0);
    int stringSize = name.length ();

    System.out.println(stringSize);
    System.out.println(upper);
    System.out.println(lower);
    System.out.println(letter);

    } // end main method

    } // end class

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: String Manipulation help!!!!!!!

    Norm gave you a link in his post at #3 in this thread. If you're going to ignore advice, why did you bother to ask on a forum?

    Go through the linked item and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Similar Threads

  1. String manipulation
    By rackd1989 in forum New To Java
    Replies: 6
    Last Post: 07-29-2012, 06:36 PM
  2. Help with String Manipulation
    By RossGoldenstein in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 04:24 AM
  3. String manipulation
    By lakshmibvaraprasad in forum New To Java
    Replies: 4
    Last Post: 07-16-2011, 01:41 PM
  4. String manipulation
    By i4ba1 in forum Advanced Java
    Replies: 2
    Last Post: 12-22-2010, 08:53 AM
  5. String Manipulation
    By tmotse in forum New To Java
    Replies: 1
    Last Post: 10-14-2010, 01:25 PM

Tags for this Thread

Posting Permissions

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