My assignment is to go through a users input and convert it to a total sum. Idea is to change all the letters to a corresponding number, as in a = 1 and so on.
Really basic stuff but I'm at a loss, my idea was to convert the users response to a char array and then loop through each char and then use a switch or multiple loops to get the value but I can't even get a for loop to work because I'm getting "Cannot invoke charAt(int) on the array type char[]".
public class question3 {
public static void main(String[] args){
Scanner userTypes = new Scanner(System.in);
String wordValue;
System.out.print("Please enter a string");
wordValue = userTypes.next();
String lowerCase;
lowerCase = wordValue.toLowerCase();
char[] arrayConvert = lowerCase.toCharArray();
System.out.println(arrayConvert);
int fullNumber;
System.out.print("Please enter an int");
fullNumber = userTypes.nextInt();
double decimalNumber;
System.out.print("Please enter a double");
decimalNumber = userTypes.nextDouble();
double totalNumber;
totalNumber = fullNumber + decimalNumber;
System.out.print("your result is " + totalNumber);
for(int i=0; i< arrayConvert.length;i++) {
if(arrayConvert.charAt(i)== ("a")){
}
}
}
char[]doesn't have a methodcharAtdefined, only String type has. You access array elements by index, egarrayConvert[i] == 'a'. But beware, in your code you use double quotes("a")and this will give you a String, not a char. Use single quotes instead, and omit the redundant braces as in'a'. If you want to compare Strings use"aSring".equals("anotherString"). It pays off getting into the details of the String/Char/Integer/Byte business.array[index]syntax.charAtbelongs toStringclass which is NOTchar[](String can containchar[]internally). But since you are already aware ofcharAtwhy not invoke it on String directly? Why do you create an array in the first place? Also"..."is String literal, but result ofcharAtischarso you should compare it with otherchar. In other wordscharAt(i)== ("a")should be more likecharAt(i)== 'a'.chartype is really a numeric type which holds number representing position of character in Unicode Table. So for instance char representing'a'really holds97. And numbers can be added to each other so to mapa->1,b->2you can calculate difference between your character and'a'and increase it by 1 like'a'-'a'+1 = 0+1 = 1'b'-'a'+1 = 1+1 = 2etc.