Answer the question
In order to leave comments, you need to log in
How to populate an ArrayList array with the scanner method from another class (java)?
I tried a lot of things by typing and this option is the most normal, but again, it’s not at all what you need. The goal is to fill the ArrayList array with a method from class 2 and calculate the sum of the array. At the end, simply issue the amount when entering Stop (accordingly, the flow should be interrupted.
1 ClassSystem.out.println(sum +" $");
import static com.company.Scanner1.input;
import java.util.ArrayList;
import java.lang.Double;
import java.lang.String;
public class Main {
public static void main(String[] args) {
ArrayList<String> Dollars1 = new ArrayList<String>();
while (true) {
String hjk = input();
Dollars1.add(hjk);
if (hjk.equals("Stop")){
System.out.println("HotGrays");
System.out.println("You chek: ");
continue;}
double[] doubleList = new double[Dollars1.size()];
double sum = 0;
for (int i = 0; i < Dollars1.size(); ++i) {
doubleList[i] = Double.parseDouble(Dollars1.get(i));
sum += doubleList[i];
}
System.out.println(sum +" $");
}
}
}
import java.util.Scanner;
import java.util.NoSuchElementException;
class Scanner1 {
static String input() {
try {
return new Scanner(System.in).nextLine();
} catch (NoSuchElementException e) {
throw e;
}
}
}
/*
import java.util.Scanner;
class Scanner1 {
public static String input() {
Scanner sum = new Scanner(System.in);
String b = null;
while (true) {
System.out.print("ItemCode: ");
String s = sum.nextLine();
if (s.equals("Stop")) {
break;
}
b = s;
}
return b;
}
}*/
Answer the question
In order to leave comments, you need to log in
Is this the condition of the task, is it confusing to do so, or is your approach? Maybe just?
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0.0;
while (scanner.hasNextDouble()) {
sum += scanner.nextDouble();
}
System.out.println(sum);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question