0

I am currently trying to complete this program and I'm having trouble with this error. I've done many things trying to fix it so I can compile it but it won't work. It seems that the "String alphabet" is getting the error. Can someone help me solve this please?

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;
    }
    else
    {
      // here is one spot that mainly the error pops up?
      alphabet[placement]=alphabet[placement]+1;
    }
  }

  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]);
    }
  }
}
2
  • Show us the error you get Commented Mar 24, 2015 at 4:15
  • The error is the title. I marked it in my code where it usually shows up Commented Mar 24, 2015 at 4:18

2 Answers 2

1

Your sort method is treating alphabet (the String) as an array. String is not a char[] but you can call String.toCharArray() like

private void sort(char toArray)
{
    char[] alpha = alphabet.toLowerCase().toCharArray();
    int placement=charToInt(toArray);
    if (placement<0)
    {
      alpha[26]=1;
    }
    else
    {
      alpha[placement]=alpha[placement]+1;
    }
    alphabet = new String(alpha, "UTF-8");
}

But modifying a String is not possible, because they are immutable. For the same reason your raw call alphabet.toLowerCase() doesn't modify the alphabet in your other method.

Sign up to request clarification or add additional context in comments.

Comments

0

The variable alphabet is defined as a String data type, but you need to define it as an array if you want to reference it using the bracket notation [] you have in your code. The error message is pretty clear in this case.

String[] example = new String[3];
example[0] = "Hello";
example[1] = "ETC...";

6 Comments

What do you mean? Can you specifically show me on my code?
Or the are after the individual character (like getCharAt) but are treating it like an array instead...possibly, maybe...
@AustinJonMagalong You define alphabet variable as a new String. You need to define it as an array. If this explanation is not clear then please consider googling Arrays and Primitives in Java then reading for a bit.
@Kon oh ok I see now. Thank you haha. That eliminated a lot of my errors. Now all I got left is error on this "alphabet[26] = 1;" It says required: java.lang.String found int.
@AustinJonMagalong The problem there is you're assigning an int data type 1 to a String[] element. You may want String[0] = "1";. Note that the 26th index will be out of bounds for an array defined like `new String[26];
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.