Skip to main content

Strings

String to take inputs for many ArrayLists

info
  • If the user input is in many lines and we have to assign the input in each line to a new ArrayList:
/* Input is:
45 565 65 65 65
4 44 5 65 66 6 64 6
4 4 54 54 44 654
*/
ArrayList<Integer> al1 = new ArrayList<>();
ArrayList<Integer> al2 = new ArrayList<>();
ArrayList<Integer> al3 = new ArrayList<>();

for (String el : sc.nextLine().split("\\s+")) { //using for-Each loop to take input from a single line as "String" and Convert it to Integer using "Integer.parseInt()" and then "added" it to ArrayList "al1"
al1.add(Integer.parseInt(el));
}

for (String el : sc.nextLine().split("\\s+")) { //using for-Each loop to take input from a single line as "String" and Convert it to Integer using "Integer.parseInt()" and then "added" it to ArrayList "al2"
al2.add(Integer.parseInt(el));
}

for (String el : sc.nextLine().split("\\s+")) { //using for-Each loop to take input from a single line as "String" and Convert it to Integer using "Integer.parseInt()" and then "added" it to ArrayList "al3"
al3.add(Integer.parseInt(el));
}

  • All the given inputs👆 have a lot of "spaces" ; So, use for - Each loop to take a whole single line as nextLine() and then split("\\s+") the line into separate strings and then use Integer.parseInt() to convert the String into Integer and then add() the converted integer to the ArrayList "al".
  • Then repeated the same way to take all other lines as separate inputs for separate ArrayLists.

Always use "\\s+" to match whitespaces

The regular expression \s or \\s matches a single whitespace character ;; while \s+ or \\s+ will match one or more whitespace characters. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/regex/Pattern.html

  • The regular expression \s is a predefined character class. It indicates a single whitespace character.
  • The plus sign + is a Greedy Quantifier, which means one or more times. For example, expression X+ matches one or more X characters.

😱Single Input First, then ArrayList input😱

caution

If the single inputs are given in the lines above the ArrayList then must add :~ String x = sc.nextLine(); before taking the nextLine as input for ArrayList.

  • Otherwise It Shows the ERROR:- NumberFormatException: For input string: ""
ArrayList<Integer> al1 = new ArrayList<>();
ArrayList<Integer> al2 = new ArrayList<>();

int n = sc.nextInt();
String x = sc.nextLine(); //👈 🤯This line must be added before taking the `nextLine()` as input for ArrayList.🤯

for (String el : sc.nextLine().split("\\s+")) { // Taking input for Arraylist by first taking "nextLine()" _ then 'split("\\s+")` ~ then converting to "Intger" by "Integer.parseInt(el)".
al1.add(Integer.parseInt(el));
}
for (String el : sc.nextLine().split("\\s+")) { // Taking input for Arraylist by first taking "nextLine()" _ then 'split("\\s+")` ~ then converting to "Intger" by "Integer.parseInt(el)".
al2.add(Integer.parseInt(el));
}

System.out.println(Collections.frequency(al1, n) == (Collections.frequency(al2, n)));

String to charAt() then Integer

Finding charAt() then converting to Integer using Character.getNumericValue()

info
  • Use charAt() to get char value of that one word String; then ~ convert that char value to Integer using Character.getNumericValue().
// Input is: [ 494964 64466 ]
String str1 = sc.next();
String str2 = sc.next();

// Question is "ADD" numbers at Index "3" of both "str1" and "str2"
int num1 = Character.getNumericValue(str1.charAt(3));
int num2 = Character.getNumericValue(str2.charAt(3));

int sum = num1 + num2;
System.out.print(sum); // prints [ 15 ]

Finding charAt() then converting to String ~ then converting to Integer

info
  • Use charAt() to get char value of that one word String; then ~ convert that char value to String using String.valueOf() ; then ~ convert that one letter String to Integer using Intger.parseInt()
// Input is: [ 494964 64466 ]
String str1 = sc.next();
String str2 = sc.next();

// Question is "ADD" numbers at Index "3" of both "str1" and "str2"
int num1 = Integer.parseInt(String.valueOf(str1.charAt(3)));
int num2 = Integer.parseInt(String.valueOf(str2.charAt(3)));

int sum = num1 + num2;
System.out.print(sum); // prints [ 15 ]

Finding charAt() then converting to Integer using charValue - '0'

info
  • Use charAt() to get char value of that one word String; then ~ convert that char value to String using String.valueOf() ; then ~ convert that one letter String to Integer using Intger.parseInt()
// Input is: [ 494964 64466 ]
String str1 = sc.next();
String str2 = sc.next();

// Question is "ADD" numbers at Index "3" of both "str1" and "str2"
int num1 = str1.charAt(3) - '0';
int num2 = str2.charAt(3) - '0';

int sum = num1 + num2;
System.out.print(sum); // prints [ 15 ]

Find no. of digits in an Integer using String.valueOf() or Integer.toString()

info
int num = 8794641;
int totalDigitsOfNumber = (String.valueOf(num)).length();
// 🤯👉 "(Integer.toString(num)).length()" can also be used👆.

System.out.println(totalDigitsOfNumber); // prints [ 7 ]

Basic String java Operations

String to Number

        String str = sc.next();
int num = Integer.parseInt(str);

Number to String

        int n = 245;
String s = Integer.toString(n);