Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Understanding String(s)

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Understanding String(s)

    Hi,

    I'm having a hard time understanding strings, how they can be compared, and how they can be used as charAt. I guess I'll present it as a problem. Let's say I am having a contest for my company and you have to enter four words on my site. So, the JOptionpane will prompt the customer/user to enter the four words. But, if there are more than four words it will loop to say they have entered too many.

    What I don't understand is how to code for something past a certain amount with Strings since if I put something with < or > I always get an error message. I'm guessing I need to convert the String but then I'm confused about how to use charAt to indicate if they entered the correct code. I know how to use charAt on it's own but am confused how to have a loop that works without also messing up the charAt. Can someone please explain this to me? (If it would be more helpful I can create a code and paste it.)

    Thanks for any advice.
    Adam


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Understanding String(s)

    Go through the String chapter in The Java Tutorial: Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings). This chapter has sections on String manipulation and comparison. Consult also the String API documentation for the operations that you can perform with a String: String (Java Platform SE 7 ).

    After that, give it a go with writing the code,. If you have trouble getting it to work, post your code here (wrapped in [code] tags) together with details of the problem and error messages.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Understanding String(s)

    Quote Originally Posted by Adam Appleby View Post
    Hi,

    I'm having a hard time understanding strings, how they can be compared, and how they can be used as charAt. I guess I'll present it as a problem. Let's say I am having a contest for my company and you have to enter four words on my site. So, the JOptionpane will prompt the customer/user to enter the four words. But, if there are more than four words it will loop to say they have entered too many.

    What I don't understand is how to code for something past a certain amount with Strings since if I put something with < or > I always get an error message. I'm guessing I need to convert the String but then I'm confused about how to use charAt to indicate if they entered the correct code. I know how to use charAt on it's own but am confused how to have a loop that works without also messing up the charAt. Can someone please explain this to me? (If it would be more helpful I can create a code and paste it.)

    Thanks for any advice.
    Adam
    It seems that you want to count the characters/words a String has, and you are trying to do it using the charAt function of String. The function's name of each object in java is a simple english and just by reading its name you'll get an idea on what it is doing, or what it can do. Just by reading the name of function chartAt, I think it is getting a character from specified index rather than counting how many characters/words does the String has.
    And if you want to know how to count its characters/words you better go to the site that jashburn provided and read all the function of String object. I'm sure you'll get there want you want.

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Understanding String(s)

    I've read the above and still don't understand. It sounds simple but I just don't get how to use a String where I can compare words since whatever I do I get an error saying there is a problem with the variable. This is my code:

    import javax.swing.*;
    import java.util.Scanner;

    public class ThreeLetterAcronym {

    public static void main(String[] arg){


    String threeWord = JOptionPane.showInputDialog(null, "Hi!"
    + "Please enter your three words to be used for "
    + "your acronym");

    Scanner input = new Scanner(threeWord);

    String[] tokens = threeWord.split("");

    if(tokens > 4)//error. Do not know how to indicate if the user put more than three words without using ">".

    char a = input.next().charAt(0);
    a = Character.toUpperCase(a);


    char b = input.next().charAt(0);
    b = Character.toUpperCase(b);


    char c = input.next().charAt(0);
    c = Character.toUpperCase(c);


    input.close();
    }

    }

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Understanding String(s)

    'tokens' is an array, not a number so you can't compare it as one, arrays do have a public field called length to indicate how many values are in it, so if(tokens.length > 4) would work.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Understanding String(s)

    So I can just use the length() to change them and complete the loop?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Understanding String(s)

    length() is a method for classes with objects
    length is a field for arrays
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Understanding String(s)

    Please post your code between code tags, not quote tags.

    From this line:

    String[] tokens = threeWord.split("");

    the variable tokens is intended to be AN ARRAY of String objects or literals. However, since the quotes in your call to the split() method are empty, I'm not sure what you'll get. Assuming the user will enter spaces between the words, add a space between the quotes.

    Then, this line

    if(tokens > 4)

    can be changed to:

    if( tokens.length > 4 ),

    but I think you mean '> 3' rather than '> 4'. And then your if statement needs a body that makes sense.

Similar Threads

  1. Need help for understanding the Program
    By padhu1989 in forum Threads
    Replies: 6
    Last Post: January 16th, 2014, 03:05 PM
  2. [SOLVED] Having problems understanding String Methods
    By kissyfurs in forum Java Theory & Questions
    Replies: 5
    Last Post: July 10th, 2013, 07:50 PM
  3. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  4. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM