Results 1 to 7 of 7
Thread: String.trim() method help.....
- 04-23-2010, 12:42 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
toUpperCase() help..... (Change of title).. New problem all the way at the bottom
I'm really confused on why i keep getting the Symbol not found compile era when i compile my code....
What the program is doing so far is basically getting info from a file and printing it on the screen. Then counting the total number of words in the passage and then printing them. In order to do that though, I'm attempting to turn the passage into an array of words.. Thats where I'm having trouble at... I'm attempting to use the trim() method and the split method, but it's not finding the trim() symbol..
The piece of code i'm trying to use is the_words = str.trim().split("\\s+");... Its at the end of the code
XML Code:public class passage { public static void main(String[] args) throws java.io.IOException { // Declare Variables int i; int num_lines = 0; String infilename; String str [] = new String [1000]; String [] the_words; // Establish keyboard input stream Scanner sc = new Scanner(System.in); // Get required data items from user // Prompt user for input file name System.out.print("Enter name of text file: "); infilename = sc.nextLine(); // Check for existence of the file File infile = new File(infilename); if(!infile.exists()) { System.out.println("The file" +infilename+ "does not exists."); System.out.println("Quitting now. Please try again."); System.exit(1); } //Open the file for reading Scanner fsc = new Scanner(infile); //Read the data in the file line by line while (fsc.hasNext() ) { str[num_lines] = fsc.nextLine(); num_lines ++; } fsc.close(); // Print text passage for(i=0; i<=num_lines; i++) System.out.println(str[i]); // Count the total number of words in passage for(i=0; i<=num_lines; i++) the_words = str[i].trim().split("\\s+"); } System.out.println(the_words.length); } }Last edited by arson09; 04-26-2010 at 07:22 PM. Reason: Need help on a different problem now
-
The error tells you what you're doing wrong -- you're trying to call trim on a String array, not on a String.
-
edit: then we can talk about the other errors that are present....
- 04-23-2010, 01:02 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
-
-
So again, call trim() on the String items in the array, not on the array itself:
Java Code:for (i = 0; i <= num_lines; i++) { the_words = str[color="red"][b][i][/b][/color].trim().split("\\s+"); System.out.println(the_words.length); }
- 04-23-2010, 01:19 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
How do you trim part of a word off from an array?
By mitty in forum New To JavaReplies: 1Last Post: 04-15-2010, 05:40 PM -
What will be output by the following code? using trim method.
By racewithferrari in forum New To JavaReplies: 4Last Post: 11-17-2009, 09:40 PM -
How to trim the value in jrxml code
By Deepa in forum New To JavaReplies: 12Last Post: 01-03-2009, 08:27 AM -
How to trim text in viewer
By diva_garg in forum SWT / JFaceReplies: 4Last Post: 08-26-2008, 02:03 PM -
String trim
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks