Results 1 to 6 of 6
  1. #1
    Bangarang is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default String class print sentence?

    Hello I'm a Java beginner and for an assignment I have to "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." I am currently stuck on the first part! I have tried absolutely everything but, I just don't understand what I'm doing wrong???
    Here is my code:

    Java Code:
    import java.util.ArrayList;
    public class String
    {
        public static void main(String[] args)
        {
            String n1 = "I";
            String n2 = "enjoy";
            String n3 = "eating";
            String n4 = "fried";
            String n5 = "chicken";
            String n6 = "on";
            String n7 = "Fridays!";
            ArrayList sentence = new ArrayList ();
            sentence.add (0, n1);
            sentence.add (1, n2);
            sentence.add (2, n3);
            sentence.add (3, n4);
            sentence.add (4, n5);
            sentence.add (5, n6);
            sentence.add (6, n7);
            System.out.print(sentence.get (0));
            System.out.print(sentence.get (1));
            System.out.print(sentence.get (2));
            System.out.print(sentence.get (3));
            System.out.print(sentence.get (4));
            System.out.print(sentence.get (5));
            System.out.print(sentence.get (6));
        }
    }
    And here is the error that is coming up!

    Java Code:
    C:\Users\Dustin\Desktop>javac String.java
    String.java:6: error: incompatible types
            String n1 = "I";
                        ^
      required: String
      found:    java.lang.String
    String.java:7: error: incompatible types
            String n2 = "enjoy";
                        ^
      required: String
      found:    java.lang.String
    String.java:8: error: incompatible types
            String n3 = "eating";
                        ^
      required: String
      found:    java.lang.String
    String.java:9: error: incompatible types
            String n4 = "fried";
                        ^
      required: String
      found:    java.lang.String
    String.java:10: error: incompatible types
            String n5 = "chicken";
                        ^
      required: String
      found:    java.lang.String
    String.java:11: error: incompatible types
            String n6 = "on";
                        ^
      required: String
      found:    java.lang.String
    String.java:12: error: incompatible types
            String n7 = "Fridays!";
                        ^
      required: String
      found:    java.lang.String
    Note: String.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    7 errors
    Any shove in the right direction would be much appreciated!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: String class print sentence?

    Change your class name. String is a major class in the JDK API. So your class is the one being used, not the java class String.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Bangarang is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: String class print sentence?

    Oh gosh, what a silly mistake! Thank you. I feel like an idiot... ahaha;;

  4. #4
    Bangarang is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: String class print sentence?

    It's still not working correctly. It says it uses unchecked or unsafe operations and to recompile with -Xlint:unchecked, so I did... here is the command prompt screen.

    Java Code:
    Microsoft Windows [Version 6.2.9200]
    (c) 2012 Microsoft Corporation. All rights reserved.
    
    C:\Users\Dustin>cd desktop
    
    C:\Users\Dustin\Desktop>javac StringAssignment.java
    Note: StringAssignment.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    
    C:\Users\Dustin\Desktop>javac -Xlint:unchecked StringAssignment.java
    StringAssignment.java:14: warning: [unchecked] unchecked call to add(int,E) as a
     member of the raw type ArrayList
            sentence.add (0, n1);
                         ^
      where E is a type-variable:
        E extends Object declared in class ArrayList
    StringAssignment.java:15: warning: [unchecked] unchecked call to add(int,E) as a
     member of the raw type ArrayList
            sentence.add (1, n2);
                         ^
      where E is a type-variable:
        E extends Object declared in class ArrayList
    StringAssignment.java:16: warning: [unchecked] unchecked call to add(int,E) as a
     member of the raw type ArrayList
            sentence.add (2, n3);
                         ^
      where E is a type-variable:
        E extends Object declared in class ArrayList
    StringAssignment.java:17: warning: [unchecked] unchecked call to add(int,E) as a
     member of the raw type ArrayList
            sentence.add (3, n4);
                         ^
      where E is a type-variable:
        E extends Object declared in class ArrayList
    StringAssignment.java:18: warning: [unchecked] unchecked call to add(int,E) as a
     member of the raw type ArrayList
            sentence.add (4, n5);
                         ^
      where E is a type-variable:
        E extends Object declared in class ArrayList
    StringAssignment.java:19: warning: [unchecked] unchecked call to add(int,E) as a
     member of the raw type ArrayList
            sentence.add (5, n6);
                         ^
      where E is a type-variable:
        E extends Object declared in class ArrayList
    StringAssignment.java:20: warning: [unchecked] unchecked call to add(int,E) as a
     member of the raw type ArrayList
            sentence.add (6, n7);
                         ^
      where E is a type-variable:
        E extends Object declared in class ArrayList
    7 warnings
    
    C:\Users\Dustin\Desktop>
    I'm not really sure what to make of all this... We never went over anything like this in class, and my teacher takes forever to respond to e-mails.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: String class print sentence?

    ArrayList is a generic type so declare it like this.
    Java Code:
    List<Integer> sentence = new ArrayList<Integer>();
    //or if you need other methods of ArrayList and not just the interface
    ArrayList<Integer>sentence = new ArrayList<Integer>();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Bangarang is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: String class print sentence?

    It is working great now! Thank you.

Similar Threads

  1. enter a string sentence
    By amorosa19 in forum New To Java
    Replies: 13
    Last Post: 09-06-2013, 11:34 AM
  2. Replies: 2
    Last Post: 09-22-2011, 01:24 AM
  3. How to break down a sentence String?
    By Georgiafbi in forum New To Java
    Replies: 8
    Last Post: 02-07-2011, 04:00 AM
  4. Print Array as String through class file?
    By ocarabal in forum New To Java
    Replies: 3
    Last Post: 06-08-2010, 06:22 AM
  5. Print multi-colored characters in a sentence
    By Jeff Smith in forum New To Java
    Replies: 1
    Last Post: 07-24-2009, 04:43 AM

Posting Permissions

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