Reputation: 80
I try to scan the input one-by-one using charAt and sorting the letters of alphabet. If there is 2 'a's in the input the list array should start with 2. If there is 1 'b' in the input list[1] = 1. If c is used 3 times list[2] = 3. I want to have this for all letters in English. For example if the input is "I bought a car." The output should be 2 1 1 0 0 0 1 1 1 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 0 Note : The method is not case sensitive.
error: method sortInput in class sorting cannot be applied to given types; in line 24 What should I do?
UPDATE : Now I get an output but I get an 26-length array which all has 0's for 26 times.
import java.util.*;
public class sorting
{
public static void main(String[] args)
{
String input;
int i;
int[] list = new int[26];
Scanner scan = new Scanner(System.in);
System.out.println("Enter an input");
input = scan.nextLine();
for(i = 0; i <= list.length; i++)
{
System.out.print(Arrays.toString(sortInput(Integer.toString(list[i]))) + " ");
}
}
public static int[] sortInput(String input)
{
input = input.toLowerCase();
char k,l;
int i, j;
String alphabet;
alphabet = "abcdefghijklmnopqrstuvwxyz";
char[] letter = new char[26];
int[] list = new int[26];
j = 0;
for(i = 0; i <= alphabet.length() - 1; i++)
{
k = alphabet.charAt(i);
letter[i] = k;
}
for(i = 0; i <= input.length() - 1; i++)
{
l = input.charAt(i);
if(letter[i] == l)
{
for(i = 0; i <= input.length() - 1; i++)
{
if(letter[i] == l)
{
j = 0;
j++;
}
}
}
list[i] = j;
}
return list;
}
}
Upvotes: 0
Views: 47
Reputation: 201447
Your instance method sortInput(String)
doesn't apply to sortInput(int)
.
This
sortInput(list[i])
could be something like
sortInput(Integer.toString(list[i]))
or change the method to take an int
. Or maybe you wanted
sortInput(input)
but it will also need to be static
(or you'll need an instance) as noted by @MikeKobit here.
Edit
Based on your comments. Pass in input
and your method should look something like
public static int[] sortInput(String input) {
input = input.toLowerCase();
int[] list = new int[26];
for (char ch : input.toCharArray()) {
if (ch >= 'a' && ch <= 'z') {
list[ch - 'a']++;
}
}
return list;
}
public static void main(String[] args) {
String input = "I bought a car.";
int[] out = sortInput(input);
for (int i : out) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
}
Edit 2
Without toCharArray()
,
public static int[] sortInput(String input) {
input = input.toLowerCase();
int[] list = new int[26];
for (int i = 0, len = input.length(); i < len; i++) {
char ch = input.charAt(i);
if (ch >= 'a' && ch <= 'z') {
list[ch - 'a']++;
}
}
return list;
}
Upvotes: 2
Reputation: 47259
You are trying to call the method sortInput
, which is an instance method, from a static context. You can either instantiate the class you are trying to call the method on or in this case it seems like you would want that method to be static
.
public static int[] sortInput(String input)
You are also trying to call that method with the incorrect type parameter.
int[] list = new int[26];
...
sortInput(list[i])
You are currently trying to call your method with an int
, not a String
.
Upvotes: 1
Reputation: 2108
Compare your
System.out.print(sortInput(list[i]) + " ");
with
public int[] sortInput(String input)
You are trying to call it with something else than it expects.
Upvotes: 1