Results 1 to 6 of 6
Thread: String class print sentence?
- 03-15-2013, 10:47 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
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:
And here is the error that is coming up!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)); } }
Any shove in the right direction would be much appreciated!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
- 03-15-2013, 11:05 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 684
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-15-2013, 11:13 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Re: String class print sentence?
Oh gosh, what a silly mistake! Thank you. I feel like an idiot... ahaha;;
- 03-15-2013, 11:26 PM #4
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
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.
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.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>
- 03-15-2013, 11:30 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 684
- Rep Power
- 1
Re: String class print sentence?
ArrayList is a generic type so declare it like this.
Regards,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>();
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-15-2013, 11:34 PM #6
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Methods in Class String - moving input text to the end of the sentence
By jakaldama in forum New To JavaReplies: 2Last Post: 09-22-2011, 12:24 AM -
How to break down a sentence String?
By Georgiafbi in forum New To JavaReplies: 8Last Post: 02-07-2011, 03:00 AM -
Print Array as String through class file?
By ocarabal in forum New To JavaReplies: 3Last Post: 06-08-2010, 05:22 AM -
Print multi-colored characters in a sentence
By Jeff Smith in forum New To JavaReplies: 1Last Post: 07-24-2009, 03:43 AM -
enter a string sentence
By amorosa19 in forum New To JavaReplies: 11Last Post: 01-28-2009, 04:05 AM


LinkBack URL
About LinkBacks

Bookmarks