Results 1 to 5 of 5
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Writing a counter

    I have been stuck for like 3 hours, trying to make a counter, a counter that counts vowels in a string array.

    Java Code:
    import pjjava.nbie.*;
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class Pojahns
    /*********************
    System.out.println ();
    System.out.print ("");
    *********************/
    {
    	public static void main (String[] args) throws Exception
    	{
    		Scanner	in = new Scanner (System.in);
    
    		String[] s1 = "Body text is the term for the text forming the main content of a book, magazine, web page or other printed matter.".split (" ");
    		arraystuff.printarray  (s1);
    		System.out.println ();
    		int counter = 0;
    
    		for (int i = 0; i < s1.length; i++)
    		{
    			for (int j = 0; j < s1[i].length (); j++)
    			{
    				char element = s1[i].get (j); //get is a method in java.util.ArrayList
    
    				switch (element)
    				{
    					case 65:
    					case 69:
    					case 73:
    					case 79:
    					case 85:
    					case 89:
    					case 97:
    					case 101:
    					case 105:
    					case 111:
    					case 117:
    					case 121:
    					{
    						counter++;
    						break;
    					}
    				}
    			}
    		}
    
    		System.out.println ("Number of vowels: " + counter);
    
    
    
    	}
    
    }
    Java Code:
    C:\New\Pojahns.java:25: cannot find symbol
    symbol  : method get(int)
    location: class java.lang.String
    				char element = s1[i].get (j); //get is a method in java.util.ArrayList
    				                    ^
    1 error
    
    Tool completed with exit code 1
    Why do I get error? Can someone explain if I use method get the wrong way?
    Last edited by Pojahn_M; 05-03-2011 at 04:55 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    The comment says it all: you're using a get() function on a String. String doesn't have a get() function.

    Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    use charAt(j) instead

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Quote Originally Posted by KevinWorkman View Post
    The comment says it all: you're using a get() function on a String. String doesn't have a get() function.

    Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    So what method should I use? I cant find anyone that is good for a counter.
    I have looked into the API for hours.

    ozzyman: I tried it at first, but it wont work for some reson.

    EDIT: Trying charAt again now worked, problem sloved.
    Last edited by Pojahn_M; 05-03-2011 at 04:55 PM.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    If it didn't work you should've posted that problem, because thats the real problem.

Similar Threads

  1. switch (counter) example please
    By erin.ctm in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 12:21 AM
  2. Increment Counter for ID
    By blkshrk81 in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 02:25 AM
  3. How to Add a Counter to a Variable?
    By Bbob in forum New To Java
    Replies: 22
    Last Post: 06-05-2010, 04:14 PM
  4. Counter
    By ks1615 in forum New To Java
    Replies: 6
    Last Post: 02-20-2009, 04:02 AM
  5. Frequency Counter
    By justlearning in forum New To Java
    Replies: 0
    Last Post: 05-07-2008, 11:50 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
  •