-3
import java.util.ArrayList;
import java.util.Scanner;

public class test {
  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter a number and when you are finished enter '=' ");
    System.out.print("Enter the first number: ");
    ArrayList<String> my_list = new ArrayList<String>();
    String value = scanner.next();
    int num = Integer.parseInt(value);
    if (num == Integer.parseInt(value)) {
        my_list.add(value);
    } else {
        System.out.print("Please enter a number");
    }
    while (!value.equals("=")) {
        System.out.print("Enter a number: ");
        value = scanner.next();
        my_list.add(value);
        my_list.remove("=");
    }
    System.out.println("The list looks like: ");
    System.out.println(my_list);
    backarryToNum(my_list);
}

public static void reverseArrayToNum(ArrayList number){
    int num;
    int sum = 0;

    for (int i = number.size()-1; i >= 0 ; i--) {
        int n = number.get(i);
        num = n * (int)Math.pow(10,i);
        sum += num;
        System.out.println(sum);
    }

  }
}

The "int n = number.get(i)" line is the problem.

The method reverseArrayToNum will make the array reverse and will make the reversed array of numbers covert to a number.

Instinct
  • 321
  • 1
  • 3
  • 16
Blen
  • 151
  • 3
  • 15
  • 1
    Please define `is the problem` – Hearner Jul 25 '18 at 12:45
  • 1
    What is `oning`? How one `ones` something? The title `How to one number of an arraylist to an int variable using get(index)?` – Yoda Jul 25 '18 at 12:53
  • Don't use raw type. `ArrayList number` should be `ArrayList number` and then this would be easier to notice that you want to parse a `String` in a `int`. Which is quite simple : [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – AxelH Jul 25 '18 at 12:59

1 Answers1

2

Just change

int n = number.get(i);

to

int n = Integer.parseInt((String) number.get(i));

otherwise you try to store a String into an int. Also, I suppose

backarryToNum(my_list);

is supposed to be

reverseArrayToNum(my_list);
Instinct
  • 321
  • 1
  • 3
  • 16