Search all uppercase letters of a string in java

Therefore, I try to find all the uppercase letters in the string entered by the user, but keep getting this runtime error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(String.java:686) at P43.main(P43.java:13) 

I feel stupid, but I just can't figure it out, and the oracle even talks about charAt on the page about java.lang.StringIndexOutOfBoundsException

Here is my code for finding capital letters and printing them:

 import java.io.*; import java.util.*; public class P43{ public static void main(String[] args){ Scanner in = new Scanner(System.in); //Uppercase String isUp = ""; System.out.print("Please give a string: "); String x = in.next(); int z = x.length(); for(int y = 0; y <= z; y++){ if(Character.isUpperCase(x.charAt(y))){ char w = x.charAt(y); isUp = isUp + w + " "; } } System.out.println("The uppercase characters are " + isUp); //Uppercase } } 

I would really appreciate any input and / or help.

+5
source share
9 answers
 for(int y = 0; y <= z; y++){ 

it should be

 for(int y = 0; y < z; y++){ 

Remember that an array index starts with ZERO.

String length returns

the number of 16-bit Unicode characters per line

Since the cycle begins with ZERO, the cycle must end at a length of-1.

+16
source

The index of the array beyond the bounds is due to the fact that the for loop does not end with length - 1 , it ends with length Most iterations for loops should be in the form:

 for (int i = 0; i < array.length; i++) { // access array[i]; } 

Same thing with the string.

Perhaps a cleaner way:

 String inputString; // get user input String outputString = ""; for (int i = 0; i < inputString.length; i++) { c = inputString.charAt(i); outputString += Character.isUpperCase(c) ? c + " " : ""; } System.out.println(outputString); 

Edit: Forgot String Does not implement Iterable<Character> , stupid Java.

+8
source

With Java 8, you can also use lambdas. Convert String to IntStream , use a filter to get only uppercase characters, and create a new String by adding filtered characters to StringBuilder :

 Scanner in = new Scanner(System.in); System.out.print("Please give a string: "); //Uppercase String isUp = in.next() .chars() .filter((c) -> Character.isUpperCase(c)) .collect(StringBuilder::new, // supplier StringBuilder::appendCodePoint, // accumulator StringBuilder::append) // combiner .toString(); System.out.println("The uppercase characters are " + isUp); //Uppercase 

Inspired:

+5
source

Try it...

Method:

 public int findUpperChar(String valitateStr) { for (int i = valitateStr.length() - 1; i >= 0; i--) { if (Character.isUpperCase(valitateStr.charAt(i))) { return i; } } return -1; } 

Application:

 String passwordStr = password.getText().toString(); ....... int len = findUpperChar(passwordStr); if ( len != -1) { capitals exist. } else { no capitals exist. } 
+3
source

Hi, one of the simple steps to search for capital char in a given string ...

Program

 import java.io.*; public class testUpper { public static void main(String args[]) throws IOException { String data,answer=""; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter any String : "); data=br.readLine(); char[] findupper=data.toCharArray(); for(int i=0;i<findupper.length;i++) { if(findupper[i]>=65&&findupper[i]<=91) //ascii value in between 65 and 91 is A to Z { answer+=findupper[i]; //adding only uppercase } } System.out.println("Answer : "+answer); } } 

Output

Enter any line:

Welcome to String WoRlD

Answer: WTHSWRD

+1
source

You can improve the readability of your code and take advantage of some other modern Java features here. Please use the Stream approach to solve this problem. I also suggest importing the least number of libraries into your class. Please avoid using. * Upon import.

 import java.util.Scanner; public class P43 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please give a string: "); String x = in.next(); x.chars().filter(c -> Character.isUpperCase(c)) .forEach(c -> System.out.print((char) c + " ")); } } 

Input Example:

saveChangesInTheEditor

Output Example:

C i TE

0
source
 import java.util.Scanner; class Demo { public static void main(String[] args) { StringBuilder s=new StringBuilder(); Scanner input = new Scanner(System.in); System.out.println("Enter your String"); String str= input.nextLine(); for(int i=0; i<str.length(); i++) { if(Character.isUpperCase(str.charAt(i))) { System.out.print(str.charAt(i)+" "); } } } } 
0
source
 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter the number"); String str= input.nextLine(); int ascii; for(int i=0; i<str.length(); i++) { ascii = str.charAt(i); System.out.println(ascii); if (ascii >= 65 && ascii <= 90) { System.out.println("captal letter found ::: "+ascii); } } } 
-1
source
 public class Cama { public static void main(String[] args) { String camal = "getStudentByName"; String temp = ""; for (int i = 0; i < camal.length(); i++) { if (Character.isUpperCase(camal.charAt(i))) { System.out.print(" " + Character.toLowerCase(camal.charAt(i))); } else if (i == 0) { System.out.print(Character.toUpperCase(camal.charAt(i))); }else{ System.out.print(camal.charAt(i)); } } } } 
-1
source

Source: https://habr.com/ru/post/1443137/


All Articles