Results 1 to 7 of 7
  1. #1
    arson09 is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default 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 08:22 PM. Reason: Need help on a different problem now

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The error tells you what you're doing wrong -- you're trying to call trim on a String array, not on a String.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    arson09 is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The error tells you what you're doing wrong -- you're trying to call trim on a String array, not on a String.

    How else could i possibly do what I'm trying to do then? In order to print the text info from the file onto the screen exactly how it's written in the file, don't i have to create the array?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by arson09 View Post
    How else could i possibly do what I'm trying to do then? In order to print the text info from the file onto the screen exactly how it's written in the file, don't i have to create the array?
    But What does it mean to call trim() on a whole array? It's meaningless. You can only call trim on the Strings held as items in the array, not on the whole array itself.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);
        }

  7. #7
    arson09 is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    LOL, but i see now that the program does have the other eras that you were talkin about..

Similar Threads

  1. Replies: 1
    Last Post: 04-15-2010, 06:40 PM
  2. What will be output by the following code? using trim method.
    By racewithferrari in forum New To Java
    Replies: 4
    Last Post: 11-17-2009, 10:40 PM
  3. How to trim the value in jrxml code
    By Deepa in forum New To Java
    Replies: 12
    Last Post: 01-03-2009, 09:27 AM
  4. How to trim text in viewer
    By diva_garg in forum SWT / JFace
    Replies: 4
    Last Post: 08-26-2008, 03:03 PM
  5. String trim
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:35 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
  •