Need Help With ArrayList remove() method.

I keep getting an error when I try to compile.

Page 1 of 1

6 Replies - 4057 Views - Last Post: 17 April 2010 - 06:15 PM Rate Topic: -----

#1 abjweb  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 09-February 10

Need Help With ArrayList remove() method.

Posted 14 April 2010 - 03:00 PM

Hello,

I'm working on an assignment for school that requires that I
:
  • Prompt the user to input a string of text
  • Determine How many valid words are in that string. Valid words meaning all letters.
  • Tell the user how many valid words are in the string.


This program uses the finch robot for output
.
That's pretty much the gist of the assignment but here are the full instructions.

Your Finch has acquired a number of skills this semester. For this skill, write a program that prompts the user for a string of input. The program then determines how many words makeup the input string. The Finch will say the input and tell how many valid words are contained in the string. A valid word must contain all letter characters. If an invalid word is found the Finch will say so and its beak will flash red. Below are examples of input dialog windows.

Bonus: 20 points, allow the user to repeat.

I haven't bothered with any output as of yet. I'm not concerned with that part of the program. What I've done so far is:

split the string into sub strings.
Put each of those sub strings into an ArrayList element,
Then looped through each substring and their individual characters to test to make sure that each character is a letter.
If a substring contains a non-letter character my goal was to remove that entire substring from the ArrayList by using the remove() method.

Unfortunately. I get this error when I compile:

cannot find symbol method remove(int) Line 43
Code Is Below

** I will also attach the file.

Any ideas as to where I went wrong and how I can fix it would be greatly appreciated. Thanks for your time and help.


import finch.*;
import java.util.*;
import javax.swing.JOptionPane;

public class LabAssign9
{

   public static void main(final String[] args)
   {
	  // Instantiating the Finch object.
      Finch stringFinch = new Finch();
      String inputString;
      boolean isSubStringAWord;
      
      inputString = JOptionPane.showInputDialog(null, "Enter a string of text.", "Word Count", JOptionPane.PLAIN_MESSAGE);
      // Splits inputString into an array of substrings.
      String[] subString = inputString.split(" ");
      
      // Loops through each index of the subString array
      for(int subStringIndex = 0; subStringIndex < subString.length; subStringIndex++)
      {
      	// Test each individual subString element's characters to see if they are all letters.
      	for(int i = 0; i < subString[subStringIndex].length(); i++)
      	{
      		if(!Character.isLetter(subString[subStringIndex].charAt(i)))
      		{
      			// Removes the subString that is not a valid word.
      			subString.remove(subStringIndex);
      			// Terminates the loop for this particular subString.  It's not neccesary to test any further.
      			i = subString[subStringIndex].length(); 
      		}
      	}
      }

      // Always end your program with finch.quit()
      stringFinch.quit();
      System.exit(0);
    }
}




Thanks again for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help With ArrayList remove() method.

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Need Help With ArrayList remove() method.

Posted 14 April 2010 - 03:05 PM

subString is a String array, not an ArrayList, and as such has no method named remove.

To make it into an ArrayList instead, declare it like this: ArrayList<String> subString = new ArrayList<String>(). YOu would also have to change any instances of subString[subStringIndex] to substring.get(subStringIndex]
Was This Post Helpful? 3
  • +
  • -

#3 abjweb  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 09-February 10

Re: Need Help With ArrayList remove() method.

Posted 14 April 2010 - 03:09 PM

Crazy, I've been racking my brain over this stupid mistake for hours.
Thanks for your help, let me see what I can do.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Need Help With ArrayList remove() method.

Posted 14 April 2010 - 03:11 PM

subString is an array of String Objects.
you cannot call the remove method from an array of Strings.
the remove method you try to use is a method in the ArrayList class.
perhaps you meant to convert your array into a List, as:
List<String> stList = Arrays.asList(subStr);

and then perform all Operations on the List.
(List length is retrieved by calling the size() method).
here is the Arrays API:
http://java.sun.com/...til/Arrays.html
and ArrayList, List API
http://java.sun.com/.../ArrayList.html
http://java.sun.com/.../util/List.html
Was This Post Helpful? 4
  • +
  • -

#5 abjweb  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 09-February 10

Re: Need Help With ArrayList remove() method.

Posted 17 April 2010 - 11:25 AM

Thank you both for your help,

Here's what I was able to come up with.
It's very heavily commented, my teacher demands a lot of descriptive comments.


/**
 * Created by: Anthony Jack
 * Lab Assignment 9
 * CIT 111
 * Date: 4.14.10
 
 Your Finch has acquired a number of skills this semester. 
 For this skill, write a program that prompts the user for a string of input.
 The program then determines how many words makeup the input string. 
 The Finch will say the input and tell how many valid words are contained in the string.
 A valid word must contain all letter characters.
 If an invalid word is found the Finch will say so and its beak will flash red.

 */

import finch.*;
import java.awt.Color;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;

public class LabAssign9
{

   public static void main(final String[] args)
   {
	  // Instantiating the Finch object.
      Finch stringFinch = new Finch();
      String inputString;
      int invalidWordCount = 0;
          

      do{
      		inputString = JOptionPane.showInputDialog(null, "Enter a string of text.", 
      			"Word Count", JOptionPane.PLAIN_MESSAGE);
      			
      		// Splits inputString into an array of subStrings.
      		String[] subString = inputString.split(" ");
      		
      		// Tell the user how many words are in the inoput string
      		stringFinch.saySomething("The string that you entered contains " +
      			subString.length + "words");
      		stringFinch.sleep(4000);
      		
      		// stringFinch repeats the user's input	
      		stringFinch.saySomething("You enterd the the string input " + inputString);
      		stringFinch.sleep(6000);
      
      		// Loops through each index of the subString array
      		for(int subStringIndex = 0; subStringIndex < subString.length; subStringIndex++)
      		{
      			// Test each individual subString element's characters to see if they are all letters.
      			for(int i = 0; i < subString[subStringIndex].length(); i++)
      			{
      				if(!Character.isLetter(subString[subStringIndex].charAt(i)))
      				{
      					stringFinch.saySomething("I've detected an invalid word in the string that you entered.");
      					stringFinch.sleep(3000);
      					
      					// stringFinch will flash his beak RED to indicated invalid input
      					for(int flasher = 0; flasher < 10; flasher++)
      					{
      						if (flasher % 2 == 0)
      						{
      							stringFinch.setLED(Color.RED);
      							stringFinch.sleep(250);
      						}
      						else
      						{
      							stringFinch.setLED(0,0,0);
      							stringFinch.sleep(250);	
      						}
    						
      					}
       					invalidWordCount -= 1;
       					/* Terminates the loop for this particular subString.  It's not neccesary to test any further.  
       					 *We've already determined that this particular subString is not a valid word */ 
      					i = subString[subStringIndex].length(); 
      				}
      			}
      		}
      	
      	// *** BONUS *** Ask the user whether or not they want to continue.
      	inputString = JOptionPane.showInputDialog(null, "Enter \"YES\" to continue.", 
      		"Repeat?", JOptionPane.QUESTION_MESSAGE);
      		
       	inputString = inputString.trim().toUpperCase();
       	
      }while(inputString.equals("YES"));
      
      // Always end your program with finch.quit()
      stringFinch.quit();
      System.exit(0);
    }
}




Thanks Again
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Need Help With ArrayList remove() method.

Posted 17 April 2010 - 05:14 PM

Commenting your code is a very good habit to get into. In the real world, you will be writing code in a team setting. Your team members will be working with your code, and you theirs; so it helps everyone when code is commented. No one wants to be working on a major component, and not be able to decipher how to use someone else's code, how it works, or other important details.
Was This Post Helpful? 2
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Need Help With ArrayList remove() method.

Posted 17 April 2010 - 06:15 PM

View Postabjweb, on 17 April 2010 - 01:25 PM, said:

Thank you both for your help,

Here's what I was able to come up with.
It's very heavily commented, my teacher demands a lot of descriptive comments.


Yah. mac is right, in my job, as a contractor, you have to comment to the extreme so that future contractors can expand on it if necessary. Though boring and seemingly useless, it can save a lot of time because you KNOW what the code does instead of trying to figure it out.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1