Jason Kim Jason Kim - 1 month ago 23
Java Question

Cannot find indexOf() symbol in Java/jGrasp

I am writing a code where the user inputs certain words into an arraylist with the wildcard (*) symbol within each word. Then, once the user is finished adding words, I ask the user for a second string and replace the * in each word with that second string.

My problem is that I am trying to use .indexOf('*') to find where * is in each word, but jGrasp keeps saying that it cannot find the symbol.

import java.util.Scanner;
import java.util.ArrayList;

class Assignment5 {
public static void main(String[] args)
Scanner scan = new Scanner (System.in);

System.out.println("Enter some words, done to stop");
ArrayList list = new ArrayList();
String e = " ";
int i;
for (i = 0; i < 100; i+=0){
e = scan.nextLine();
if (e.equals("done")){
i = 100;}

System.out.println("Enter the replacement string");
String s = scan.nextLine();
int w;
int x;
for (w = 0; w < list.size(); w++){
x = list.get(w).indexOf('*')
if (x != -1){
list.set(x, s);}



Any and all help will be appreciated. I looked through the forum and was unable to find any answers that fit my situation. Also, this is my first post so I apologize for any mistakes etc!


Your ArrayList list is a non-generic ArrayList of Object, and that's all it knows that it holds. Yes it may actually hold Strings, but the ArrayList itself doesn't know this, and so the compiler only knows that list.get(w) returns an Object and not a String type. Since Object has no indexOf(...) method, you're running into problems. You have two possible solutions

  1. Use a cast to cast the object returned to String:

 x = ((String) list.get(w)).indexOf('*')`
  1. Or use a generic list: List<String> list = new ArrayList<>();, where know the list knows that it holds Strings and that get(w) will return a String object. This second way is cleaner, since this way the compiler won't allow you to add non-String objects to the list, making the code safer and less prone to bugs.