Reputation: 13
I am trying to make a program, that lets me type in 10 characters and stores them in an array. just single characters are enough, for example (d, s, a, e, h, j, e,). and then lets me look for one of the chars using linear search algorithm and gives out the position within the array.
I tried to program it but I can only do it with integers. here is my code so far.
I don't know how to change it to letters / characters?
public static void main(String args[])
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " Letters");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("What letter do you want to find?");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
if (c == n) /* Searching element is absent */
System.out.println(search + " Letter is not found.");
Upvotes: 1
Views: 7317
Reputation: 2079
I tried to program it but I can only do it with integers
No, you can do it if you use in.next().charAt(0)
rather than in.nextInt()
from Scanner
Class to take the first Character
form String
.
I don't know how to change it to letters / characters?
Here you don't need to change it, or some regex
or split
it, the method in.next()
get the String from the input (end-use) and then get the charAt(0)
the first one.
Now, what i have changed in your code to be worked as you mentioned above:
search
and array[]
to char
Data type.array[]
to array = new char[n]
search
and array
to in.next().charAt(0)
.Try this:
public static void main(String args[]) {
int n,c;
char search,array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new char[n];
System.out.println("Enter " + n + " Letters");
for ( c = 0; c < n; c++) {
array[c] = in.next().charAt(0);
}
System.out.println("What letter do you want to find?");
search = in.next().charAt(0);
for ( c = 0; c < n; c++) {
if (array[c] == search) /* Searching element is present */ {
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
if (c == n) /* Searching element is absent */ {
System.out.println(search + " Letter is not found.");
}
}
}
Upvotes: 5
Reputation: 14269
Here is a simpler approach, using Java's built-in functionality:
final Scanner in = new Scanner(System.in);
System.out.println("Please enter a series of characters:");
final String input = in.next();
System.out.println("What do you want to find?");
final String search = in.next();
int pos = input.indexOf(search);
if (pos < 0) {
System.out.println("No match.");
} else {
while (pos >= 0) {
System.out.println("Found at location: " + pos);
pos = input.indexOf(search, pos + 1);
}
}
Differences:
There is only the scanning of the actual characters, because the length of the input defines how many characters the user actually wants to input.
The indexOf() method does about the same as your function, but it's faster and easier (because you don't actually need to code it).
This allows the user to scan for more than just one char. If that is not what is desired, I will leave it up to you to figure out how to change that. ;)
Upvotes: 0
Reputation: 1243
The problem resides in your line array[c] = in.nextInt();
. The nextInt() method
of the Scanner class can read an integer, and only an integer.
You could try using array[c] = in.nextLine().charAt(0);
, which would take any input from the console, store it temporarily as a String, get that String's first character, and store that. You would need to do the same to your second in.nextInt()
.
As an aside, it's generally bad practice to store char
's in int
's, even though it is valid Java. For example, I would change c
and search
to char
's, and change array
to a char[]
.
Upvotes: 0