如何将多个String-Arraylist存储到int变量?

时间:2018-07-25 12:42:17

标签: java

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);
    }

  }
}

问题是“ int n = number.get(i)”行。

reverseArrayToNum方法将使数组反向,并使反向的数字数组转换为数字。

1 个答案:

答案 0 :(得分:2)

只是改变

Caption

int n = number.get(i);

否则,您尝试将String存储到int中。 而且,我想

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

应该是

backarryToNum(my_list);