Thursday, March 5, 2015

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?































No comments:

Post a Comment