O
O
Oleg Carp2019-04-21 22:34:37
Java
Oleg Carp, 2019-04-21 22:34:37

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 Class
System.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 +" $");

            }

        }
    }

2nd grade scanner
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;

        }
    }
}

This is a different version of Scanner
/*
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

1 answer(s)
N
Natalia, 2019-04-22
@Rebel-Cat

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 question

Ask a Question

731 491 924 answers to any question