2

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")){

            }
        }
    }
4
  • 2
    char[] doesn't have a method charAt defined, only String type has. You access array elements by index, eg arrayConvert[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. Commented Nov 2, 2019 at 16:11
  • Arrays allow us to access their content via array[index] syntax. charAt belongs to String class which is NOT char[] (String can contain char[] internally). But since you are already aware of charAt why not invoke it on String directly? Why do you create an array in the first place? Also "..." is String literal, but result of charAt is char so you should compare it with other char. In other words charAt(i)== ("a") should be more like charAt(i)== 'a'. Commented Nov 2, 2019 at 16:14
  • Thanks, spot on with your answer Commented Nov 2, 2019 at 16:16
  • BTW char type is really a numeric type which holds number representing position of character in Unicode Table. So for instance char representing 'a' really holds 97. And numbers can be added to each other so to map a->1, b->2 you 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 = 2 etc. Commented Nov 2, 2019 at 16:22

2 Answers 2

2

I didn't test your code, but charAt isn't a char[] method. Try this:

for(int i=0; i< arrayConvert.length;i++) {
    if(arrayConvert[i] == 'a'){

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Got 'Incompatible operand types char and String', actually i changed to single quotes and it seems to work, cheers
Cheers, pal, but still it would pay off to get familiar with the difference between a String and a char (and a byte perhaps), that's a very common issue.
0

My first question, Why do you want to get a string and converting into int or long. It looks you need to do code for all the inputs (i,e- if you want to get 1000 values as inputs then you need to write code stdout and next* operation for 1000 times.

Try the below one which will help users to choose their choice of inputs and get the sum value of those inputs.

SumGivenInputs.java

import java.util.Arrays;
import java.util.Scanner;

public class SumGivenInputs {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter the number of inputs\n");
       int noOfInput = scan.nextInt();

       System.out.printf("Enter the %d input%s \n", noOfInput, noOfInput > 1 ? "s one by one" : "");
       Integer[] inputList = new Integer[noOfInput];
       for (int i = 0; i < noOfInput; i++) {
           inputList[i] = scan.nextInt();
       }
       int result = 0;
       for (Integer input : Arrays.asList(inputList)) {
           result += input;
       }
       System.out.printf("Sum = %s", result);
   }
}

Output:

Enter the number of inputs
5
Enter the 5 inputs one by one 
10
20
30
5
50
Sum = 115

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.