Austin Jon Magalong
Austin Jon Magalong

Reputation: 21

Java Error - array required, but java.lang.String found

I've been working on this problem for a while and managed to get rid of almost all the errors on this program. Every time I compile I seem to get this error saying "array required, but java.lang.String found." I'm really confused on what this means. Can someone help me please? I've been struggling a lot.

import java.util.Scanner;
public class Period
{
  private static String phrase;
  private static String alphabet;
  public static void main(String [] args)
  {
    Scanner keyboard = new Scanner(System.in);
    String userInput;
    int[] letter = new int [27];
    int number = keyboard.nextInt();
    System.out.println("Enter a sentence with a period at the end.");
    userInput = keyboard.nextLine();
    userInput.toLowerCase();
  }

  public void Sorter(String newPhrase)
  {
    phrase=newPhrase.substring(0,newPhrase.indexOf("."));
  }

  private int charToInt(char currentLetter)
  {
    int converted=(int)currentLetter-(int)'a';
    return converted;
  }

  private void writeToArray()
  {
    char next;
    for (int i=0;i<phrase.length();i++)
    {
      next=(char)phrase.charAt(i);
      sort(next);
    }
  }

  private String cutPhrase()
  {
    phrase=phrase.substring(0,phrase.indexOf("."));
    return phrase;
  }

  private void sort(char toArray)
  {
    int placement=charToInt(toArray);
    if (placement<0)
    {
      alphabet[26]=1; // This is here the error occurs.
    }
    else
    {
      alphabet[placement] = alphabet[placement] + 1; // This is where the error occurs.
    }
  }

  public void entryPoint()
  {
    writeToArray();
    displaySorted();
  }

  private void displaySorted()
  {
    for (int q=0; q<26;q++)
    {
      System.out.println("Number of " + (char)('a'+q) +"'s: "+alphabet[q]); //this is where the error occurs.
    }
  }
}

Upvotes: 0

Views: 2441

Answers (3)

Bethany Louise
Bethany Louise

Reputation: 646

You can't use a String as an array. There are two options here to fix this:

1) Make alphabet char[] instead of String.

  • or

2) Don't treat alphabet like an array. Instead of trying to reference a character as if it was stored in an array, use alphabet.charAt(placement). You can't use charAt() to replace one character with another, though, so instead of:

alphabet[placement] = alphabet[placement] + 1;

use this:

alphabet = alphabet.substring(0, placement+1) + "1" + alphabet.substring(placement+1);

That's assuming you want to insert "1" after the specified character in alphabet (it isn't entirely clear to me what you're trying to achieve here). If you meant instead to have that line of code replace the character you've referred to as alphabet[placement] with the one that follows it, you would want to do this instead:

alphabet = alphabet.substring(0, placement+1) + alphabet.charAt(placement+1) + alphabet.substring(placement+1);

Alternatively, you could set alphabet to be a StringBuilder rather than a String to make it easier to modify. If alphabet is a StringBuilder, then the first alternative to the line in question (inserting "1") could be written like this:

alphabet = alphabet.insert(placement, 1);

The second alternative (changing alphabet.charAt(placement) to match the following character could be written like this:

alphabet.setCharAt(placement, alphabet.charAt(placement+1));

Upvotes: 1

Ehsan
Ehsan

Reputation: 4464

Well, the problem is that you cannot threat String in java like an array (e.g., alphabet[i]).

String are immutable in Java. You can't change them.

You need to create a new string with the character replaced.

String myName = "domanokz";
String newName = myName.substring(0,4)+'x'+myName.substring(5);

Or you can use a StringBuilder:

StringBuilder myName = new StringBuilder("domanokz");
myName.setCharAt(4, 'x');

System.out.println(myName);

If I were you, I would have used the second method.

Upvotes: 0

RockAndRoll
RockAndRoll

Reputation: 2277

replace

private static String alphabet;

with

private static char[] alphabet = new char [27];//to keep it in sync with letter

it should work.

Upvotes: 1

Related Questions