I am trying to write a command line program that accepts inputs. If the input is an integer , it squares every digit of the number and concatenate them but if the input is not an integer then it asks the user to input a digit. But the output is wrong. Please Check the code.
package com.company;
import java.util.*;
public class ExerciseB {
public static void main (String[] args) {
while (true) {
System.out.println("Enter a digit or 'quit' to quit: ");
Scanner userInput = new Scanner(System.in);
if (userInput.hasNextInt()) {
int intInput = userInput.nextInt();
int i;
int intCharSquare;
ArrayList<Integer> valueCharSquare = new ArrayList<>();
//Generating the square of each character of the inputted integer and adding it to the valueCharSquare ArrayList
String stringOfIntInput= String.valueOf(intInput);
for (i = 0; i <= stringOfIntInput.length() - 1; i++) {
intCharSquare = (int) Math.pow((int) stringOfIntInput.charAt(i),2);
valueCharSquare.add(intCharSquare);
}
// Storing the first value of the valueCharSquare ArrayList into a variable
int result = valueCharSquare.get(0);
//Converting the value of result into a string
String.valueOf(result);
/* Obtaining the other values of the valueCharSquare ArrayList
* and storing them into otherResult variable
* then converting otherResult into a string
* then concatenating the values of result and otherResults
*/
for (i = 1; i <= valueCharSquare.size() - 1; i++) {
int otherResults = valueCharSquare.get(i);
String.valueOf(otherResults);
result = result + otherResults;
}
//printing out new value of result
System.out.println(result);
// This happens if the user input is a string
} else if (userInput.hasNext()) {
String stringInput = userInput.nextLine();
if (stringInput.equals("quit")) {
break;
} else {
continue;
}
}
}
}
}