0

I'm trying to add elements to two arrays. I declared the double arrays so that they also accept decimal numbers.

I am getting 7 elements from the user and add them. If the user only gives 6 elements instead of 7 elements, it waits until the 7th element is given.

If the user gives only 6 elements instead of giving 7, it has to take the position of 7th element as "0" and add the elements and print the sum. It cannot wait for the 7th element. For that what can i do?

        Scanner in = new Scanner(System.in);
        double a[] =new double[100];
        double b[] =new double[100];
        double suma=0,sumb=0;
        for(int i=0;i<7;i++)
        {
            a[i]=in.nextDouble();
            suma=suma+a[i];
        }
        for(int i=0;i<7;i++)
        {
            b[i]=in.nextDouble();
            sumb=sumb+b[i];
        }
3
  • 1
    Hint: You need to find a way to let the user input something which tells the program that he does not intend to input any more numbers. Commented Jun 12, 2019 at 5:34
  • 1
    How is the input being provided? Is it like 1 2 3 4 5 6 7 in a single line or single integer in each line? Commented Jun 12, 2019 at 5:36
  • it"s like 1 2 3 4 5 6 7 (single line) Commented Jun 12, 2019 at 5:43

2 Answers 2

4

We can use something like this

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String strArr[] = br.readLine().split(" ");
double a[]=new double[100];
double suma=0;
for(int i=0;i<strArr.length;i++)
{
  a[i] = Double.parseDouble(strArr[i]);
  suma += a[i];
}

In this way, if only 6 values are provided then an array will have values from index 0 to 5 and rest all the values will remain 0

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a specific delimiter to exclude the new line that is the default. That way, you can use hasNextDouble to loop until you reach the end of the line.

    Scanner in =new Scanner(System.in);
    double a[]=new double[10];
    double b[]=new double[10];
    double suma=0,sumb=0;

    //Use space as delimiter only, this accept a regex
    in.useDelimiter("[ ]+")
        .useLocale(Locale.ENGLISH);

    //Loop until the line is out of value (separated by spaces)
    for(int i=0; in.hasNextDouble(); i++)
    {
        a[i]=in.nextDouble();
        suma=suma+a[i];
    }

    //Consume the newline still in the buffer
    in.nextLine();

    //Loop until the line is out of value (separated by spaces)
    for(int i=0; in.hasNextDouble(); i++)
    {
        b[i]=in.nextDouble();
        sumb += b[i];
    }

    System.out.println("Sum of " + Arrays.toString(a) + " = " + suma);
    System.out.println("Sum of " + Arrays.toString(b) + " = " + sumb);

Example :

Sum of [1.1, 5.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] = 6.5
Sum of [4.8, 7.4, 1.5, 4.8, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0] = 18.6

Note that this could be improved, if using a file, using two loops to read everything and put into lists.

    Scanner in = new Scanner("1.4 2.2 3.1\n1.8 7.4 24.5");
    List<List<Double>> arrays = new ArrayList<>();

    in.useDelimiter("[ ]+")
        .useLocale(Locale.ENGLISH);

    List<Double> tmp;
    do{
        tmp = new ArrayList<>();
        arrays.add(tmp);
        while (in.hasNextDouble()) {
            tmp.add(in.nextDouble());
        }
        System.out.println(tmp);
        System.out.println(tmp.stream().mapToDouble(d -> Double.valueOf(d)).sum());
    } while (in.hasNextLine() && in.nextLine() != null);

[1.4, 2.2]
3.6
[1.8, 7.4, 24.5]
33.7

2 Comments

It works for integer only.But it fails for the number which is given as decimal number.
I made a mistake on the pattern @Harini, using a ?. I have corrected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.