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 9 of 9

Thread: Help with my code please!

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with my code please!

    Every time I compile this program, it always says there's an error, and I feel like I've done everything right. Currently it's saying that "sentence.add(str1);" has an error because it can't find a symbol? And also the project says that this program should be using the String Class but it wants us to list the words in an Arraylist. When I used the String Class first, it couldn't compile because I wasn't using an Arraylist Class even though I used an Arraylist. So how can we even use it in a String Class if there's an Arraylist? HELP, please.

    Here is the code:

    class Arraylist{
    public static void main(String [] args){
    Arraylist sentence = new Arraylist();
    String str1 = "Finally";
    String str2 = "the";
    String str3 = "weather";
    String str4 = "is";
    String str5 = "warm!";
    sentence.add(str1);
    sentence.add(str2);
    sentence.add(str3);
    sentence.add(str4);
    sentence.add(str5);
    System.out.println(sentence);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with my code please!

    Please copy the full text of the compiler's error messages and post it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Help with my code please!

    Your problem here is that you are creating your own arraylist object. Is there a reason you are not using the ArrayList class from the java.util package?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code please!

    Here are the directions to my assignment:

    Write two programs: one using the String class and one using the StringBuffer class. Your programs should store a set of Strings in an ArrayList and print those Strings in the order by which they are added. The output of your programs should create a complete sentence.

    Am I completely off base with my code? I am new to Java and I appreciate all the help

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with my code please!

    One problem with the code (as Chris said) is the use of a name (Arraylist) that is very similar to a java SE class: ArrayList.

    If you are getting errors, post the full text here.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Help with my code please!

    I think the issue you are having is that you did not realize that ArrayList is a class that already exists. Head over to google and look it up. It's a great class that you will use for years to come, might as well get acquainted with it.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code please!

    Thank you all so much for the advice! I have made a few changes and I am still receiving errors. Here is the updated copy of my code. Any ideas?

    class test{
    public static void main(String [] args){
    sentence = new Arraylist();
    String str1 = "Finally";
    String str2 = "the";
    String str3 = "weather";
    String str4 = "is";
    String str5 = "warm!";
    sentence.add(str1);
    sentence.add(str2);
    sentence.add(str3);
    sentence.add(str4);
    sentence.add(str5);
    System.out.println(sentence);
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with my code please!

    I am still receiving errors.
    Please copy the full text of the error messages and post it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code please!

    Thank you all again for the help! I figured out the problem

Similar Threads

  1. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  2. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  3. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM