I need to ask the user to input a sentence and then a letter. The program should then print out how many letters the sentence contains. Also the index position of the specified character that the user inputted. My problem is that I don’t know how to find the position of that character.
NOTE: I have searched the web for answer.
JavaScript
x
import javax.swing.*;
public class Main {
public static void main(String[] args) {
String sentence; //Store the users senctence
String sentence2; //Stores the letter that the user wants to count.
int index;
sentence = JOptionPane.showInputDialog("Write a sentence");
sentence2 = JOptionPane.showInputDialog("Write a letter");
int sLenght = 0;
int countCha = sentence2.indexOf(sentence2);
if (sentence == null || sentence.equals(""))
JOptionPane.showMessageDialog(null, "You need to input a sentence to continue");
else {
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) != 1)
sLenght++;
}
JOptionPane.showMessageDialog(null, "The sentence contains" + " " + sLenght +
" " + "characters" + "n" + "Tecknet" + " " + sentence2 + " " + "occurs" + sentence.indexOf(sentence2) + " " + "times");
}
}
}
Advertisement
Answer
If you want to show just the first index where the character is found, you can use String#indexOf(int ch)
. If you want to display all the positions where the letter occurs in the sentence, you can use String#indexOf(String str, int fromIndex)
.
Demo:
JavaScript
public class Main {
public static void main(String[] args) {
String sentence = "Hello world!";
char ch = 'l';
int index = sentence.indexOf(ch);
if (index != -1) {
System.out.println("The first occurance of '" + ch + "' is at " + index);
} else {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
}
// All positions
System.out.println("All positions: ");
int fromIndex = 0, count = 0;
for (int i = 0; i < sentence.length(); i++) {
index = sentence.indexOf(ch, fromIndex);
if (index != -1) {
System.out.println("'" + ch + "' was found at " + index);
fromIndex = index + 1;
count++;
}
}
if (count == 0) {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
}
}
}
Output:
JavaScript
The first occurance of 'l' is at 2
All positions:
'l' was found at 2
'l' was found at 3
'l' was found at 9
Alternatively, you can use String#charAt
:
JavaScript
public class Main {
public static void main(String[] args) {
String sentence = "Hello world!";
char ch = 'l';
int count = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ch) {
System.out.println("'" + ch + "' was found at " + i);
count++;
}
}
if (count == 0) {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
}
}
}
Output:
JavaScript
'l' was found at 2
'l' was found at 3
'l' was found at 9
You can also add all the positions to a List<Integer>
and display the same.
JavaScript
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String sentence = "Hello world!";
char ch = 'l';
List<Integer> positions = new ArrayList<>();
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ch) {
positions.add(i);
}
}
if (positions.size() == 0) {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
} else {
System.out.println("The positions where '" + ch + "' was found is/are " + positions);
}
}
}
Output:
JavaScript
The positions where 'l' was found is/are [2, 3, 9]