Results 1 to 7 of 7
  1. #1
    FermataPlays is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Question Strings and StringBuffer Project

    So, I've been working on a school project for a couple days, and I have my code written out but I can't fix the compile-time errors.

    The prompt for it is here:
    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.

    I am stuck on the first program.

    Here's my code for the first program:
    Java Code:
    import java.util.ArrayList;
    
    public class SentenceNormal {
    	public static void main(String args[]) {
    		String n1 = "My ";
    		String n2 = "favorite ";
    		String n3 = "football ";
    		String n4 = "team ";
    		String n5 = "is ";
    		String n6 = "the ";
    		String n7 = "Seahawks";
    		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);
    		for (int i=0; i<7; i++) {
    			System.out.print(sentence.get(i));
    		}
    		System.out.print(".");
    	}
    }
    The first error I get says this:

    C:\Users\Lauren\Dropbox\Schoolwork\9th Grade\Java Programming\Unit 4 - Strings\U
    nit Project>javac SentenceNormal.java
    Note: SentenceNormal.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.


    After I add the "-Xlint" I get this:

    C:\Users\Lauren\Dropbox\Schoolwork\9th Grade\Java Programming\Unit 4 - Strings\U
    nit Project>javac -Xlint SentenceNormal.java
    SentenceNormal.java:12: warning: [rawtypes] found raw type: ArrayList
    ArrayList sentence = new ArrayList();
    ^
    missing type arguments for generic class ArrayList<E>
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:12: warning: [rawtypes] found raw type: ArrayList
    ArrayList sentence = new ArrayList();
    ^
    missing type arguments for generic class ArrayList<E>
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:13: warning: [unchecked] unchecked call to add(int,E) as a m
    ember of the raw type ArrayList
    sentence.add(0,n1);
    ^
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:14: warning: [unchecked] unchecked call to add(int,E) as a m
    ember of the raw type ArrayList
    sentence.add(1,n2);
    ^
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:15: warning: [unchecked] unchecked call to add(int,E) as a m
    ember of the raw type ArrayList
    sentence.add(2,n3);
    ^
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:16: warning: [unchecked] unchecked call to add(int,E) as a m
    ember of the raw type ArrayList
    sentence.add(3,n4);
    ^
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:17: warning: [unchecked] unchecked call to add(int,E) as a m
    ember of the raw type ArrayList
    sentence.add(4,n5);
    ^
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:18: warning: [unchecked] unchecked call to add(int,E) as a m
    ember of the raw type ArrayList
    sentence.add(5,n6);
    ^
    where E is a type-variable:
    E extends Object declared in class ArrayList
    SentenceNormal.java:19: warning: [unchecked] unchecked call to add(int,E) as a m
    ember of the raw type ArrayList
    sentence.add(6,n7);
    ^
    where E is a type-variable:
    E extends Object declared in class ArrayList
    9 warnings


    I honestly have no clue what to do, as my syntax looks right to me. I am still semi-new to Java so I have no idea what is wrong with my code.
    Anyone help?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Strings and StringBuffer Project

    These are warnings, not errors. Your program should run just fine. Java would like you to tell the ArrayList what type you will put in the list. We call that "generics". If you add that, the warnings go away:
    Java Code:
    ArrayList<String> sentence = new ArrayList<>();
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Strings and StringBuffer Project

    And except in special circumstances, it is considered best practice to assign the instance of the implementation to the type. So since ArrayList is an implementation of the List interface, it would be:

    Java Code:
    List<String> sentence = new ArrayList<>();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    FermataPlays is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Default Re: Strings and StringBuffer Project

    Wow that is an easy fix. Thanks guys!

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Strings and StringBuffer Project

    Quote Originally Posted by jim829 View Post
    And except in special circumstances, it is considered best practice to assign the instance of the implementation to the type. So since ArrayList is an implementation of the List interface, it would be:

    Java Code:
    List<String> sentence = new ArrayList<>();
    Regards,
    Jim
    I was gonna add that, but then I thought it might be leading to more questions than necessary. I even coined the term "design by contract". :) You worded it better than me anyway.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Strings and StringBuffer Project

    The additional question would hopefully be "why?" - better add the answer to that directly to the best practice or whatever piece of additional information you provide - at least when posting in New To Java.

    Just my 2 cents, I'm not pointing fingers ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Strings and StringBuffer Project

    Always question the man!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Using StringBuffer
    By LuluMM in forum New To Java
    Replies: 2
    Last Post: 05-23-2012, 11:03 PM
  2. How to use a StringBuffer?
    By Hallowed in forum New To Java
    Replies: 5
    Last Post: 02-18-2011, 05:38 AM
  3. Replies: 3
    Last Post: 03-04-2009, 06:03 AM
  4. Replies: 1
    Last Post: 03-03-2009, 05:27 AM
  5. Should I use StringBuffer?
    By GenkiSudo in forum New To Java
    Replies: 7
    Last Post: 09-21-2008, 12:39 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
  •