简体   繁体   中英

Cannot convert from double to int error + Type of expression must be an array type but it resolved to type <String> error

I'm trying to get a total for a number of transactions from an array, however i had two errors. 1- Cannot convert from double to int 2- Type of expression must be an array type but it resolved to type String

Here's my code:

List<String> transactions = new ArrayList<String>();
List<String> amount = new ArrayList<String>();

do {
    System.out.println("Enter a Transaction: ");
    Scanner tInput = new Scanner(System.in);

    String a = tInput.nextLine();
    transactions.add(a);
    System.out.println("Enter purchase amount: ");
    double b = tInput.nextDouble();

    amount.add(b);
    System.out.println("Do you have new record ?(yes/no)");
    String answer = tInput.nextLine();

    if (answer.equalsIgnoreCase("Yes")) {

        continue;
    }
    break;
} while (true);
for (int i = 0; i < transactions.size(); i++) {

    System.out.println(transactions.get(i));
}
System.out.println(transactions + " (£)" + amount);

System.out.println("Input Camper First Name: ");
Scanner afn = new Scanner(System.in);
String af = afn.nextLine();

System.out.println("Input Camper Surname: ");
Scanner aln = new Scanner(System.in);
String al = afn.nextLine();

NewCamper.first = af;
NewCamper.last = al;
System.out.println(af + " " + al + " 's transactions:" + transactions);
System.out.println(af + " " + al + " 's amount:" + amount);

double total;
for (double counter = 0; counter < amount.size(); counter++) {
    total += amount[counter];
}

Any tips would be appreciated. Thanks in advance

You are trying to add a double type in a List. It will throw a cast exception. It won't allow this-:

double b = tInput.nextDouble(); amount.add(b);

You need to change your String List to Double Listand the instantiation according to it.

public static void main(String args[]) {
    do {
        System.out.println("Credit Camper Account? (yes/no)");

        Scanner q = new Scanner(System.in);
        String uq = q.nextLine();

        if (uq.equalsIgnoreCase("yes")) {
            System.out.println("How much to credit?");
            double c = q.nextDouble();
            Account camp1 = new Account();
            camp1.deposit(c);

            System.out.println("Camper Credit: " + c);
        } else if (uq.equalsIgnoreCase("no")) {
            System.out.println(Account.balance);
        } else {
            System.out.println("Invalid Response");
            continue;
        }
        break;
    } while (true);

    List<String> transactions = new ArrayList<String>();
    List<Double> amount = new ArrayList<>();

    do {
        System.out.println("Enter a Transaction: ");
        Scanner tInput = new Scanner(System.in);
        Scanner dInput = new Scanner(System.in);
        String a = tInput.nextLine();

        transactions.add(a);
        System.out.println("Enter purchase amount: ");

        double b = dInput.nextDouble();

        amount.add(b);
        System.out.println("Do you have new record ?(yes/no)");
        String answer = tInput.nextLine();

        if (answer.equalsIgnoreCase("Yes")) {
            continue;
        }
        break;
    } while (true);
    for (int i = 0; i < transactions.size(); i++) {

        System.out.println(transactions.get(i));
    }
    System.out.println(transactions + " (£)" + amount);

    System.out.println("Input Camper First Name: ");
    Scanner afn = new Scanner(System.in);
    String af = afn.nextLine();

    System.out.println("Input Camper Surname: ");
    Scanner aln = new Scanner(System.in);
    String al = afn.nextLine();

    NewCamper.first = af;
    NewCamper.last = al;
    System.out.println(af + " " + al + " 's transactions:" + transactions);
    System.out.println(af + " " + al + " 's purchase amounts:" + amount);

    double total = 0.0d;
    for (int counter = 0; counter < amount.size(); counter++) {
        total += amount.get(counter);

    }
}

I Just needed to create a seperate Scanner since i changed from line to double....

:D....thanks all

First, change amount to the List of double as in comment. Amount is an ArrayList, but you trying to access it as an array.

  • change total += amount[counter]; to total += amount.get(counter);
  • you does't need double counter: for (int counter = 0; counter < amount.size(); counter++)
  • you even can use foreach:

    for (double element : amount){ total+=element; }

First, you are trying to add a double to a list of String type

double b = tInput.nextDouble();

amount.add(b);

which is not allowed. You can find why so here . This can be done like,

amount.add("" + b);

or, maybe

amount.add(String.valueOf(b));

Second, you are trying to access element of a list in array-fashion. More so you are using a double variable as counter. You have to use integer counter for array-fashion like access,

for (int counter = 0; counter < amount.size(); counter++) {
    total += Double.parseDouble(amount.get(counter));           
}

Notice that you have to extract your list element value as 'Double', since you have chosen a list of String.

As pointed out by others, the best thing to do is change your list type to Double .

List<Double> amount = new ArrayList<>();

This way your code needs minimum changes. Only last few lines will become

for (int counter = 0; counter < amount.size(); counter++) {
    total += amount.get(counter);   
}

Getting a fair understanding of Generics is highly recommended for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM