-
Code:
ArrayList<String> mm = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = input.nextLine().trim();
System.out.println("Enter an index: ");
int index = input.nextInt();
mm[index] = str; <---
System.out.println(mm);
System.out.println(index);
for (int k = 0; k < size; k++)
System.out.println(mm[k]); <--
Why won't this work?
array required, but java.util.ArrayList<java.lang.String> found
array required, but java.util.ArrayList<java.lang.String> found
errors occur at arrows.
-
-
Because mm is an ArrayList not an array :). This means you cannot use the mm[idx] syntax. Arrays are special objects in Java. On the other hand the ArrayList is just a list object backed by an array :)
Use this code to compare the two concepts:
Code:
public class ArrayTest
{
public static void main(String[] args)
{
useArray();
useList();
}
private static void useList()
{
ArrayList<String> mm = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = input.nextLine().trim();
mm.add(str);
System.out.println(mm);
for (int k = 0; k < mm.size(); k++)
{
System.out.println("mm(" + k + ") = " + mm.get(k));
}
}
private static void useArray()
{
String[] mm = new String[10];
Scanner input = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = input.nextLine().trim();
System.out.println("Enter an index: ");
int index = input.nextInt();
if (index < mm.length)
{
mm[index] = str;
}
for (int k = 0; k < mm.length; k++)
{
System.out.println("mm[" + k + "] = " + mm[k]);
}
}
}
-
So I've decided to abandon the array ship, and just use the different String methods.
Here's what I have.
Code:
import java.util.*;
public class p499proj12l2 {
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter word: ");
String sent = input.nextLine();
String trimsent = sent.trim();
int length = trimsent.length();
int gate;
int index;
do{
System.out.println("Enter index wanted: ");
index = input.nextInt();
int b = length - 1;
if (index > b)
{
System.out.println("Index doesn't exist");
gate = 0;
}
else
{
gate = 1;
}
}
while(gate != 1);
Recurse(trimsent, index, length);
}
public static String Recurse(String trimsent, int index, int length)
{
if(index <= length)
{
Recurse(trimsent, index + 1 , length);
char chars = trimsent.charAt(index);
System.out.print(chars);
}
else
{
System.out.println();
System.out.println(trimsent);
}
return null;
}
}
What the code is supposed to do is ask for a word and an index. At the entered index the word is supposed to be spelled backwards from that spot using a recursive method.
i.e. shoe index 1 = seoh.
I'm having trouble just with my Recurse method.
Because for some reason I get these results...
Shoe , index 1 = eohs
Shoe , index 2 = eoh
Shoe , index 3 = eo
Now I see the pattern, but I'm confused on how it's getting there.
My else statement always runs first too for some reason unbeknownst to me...