Home > Blockchain >  Int array pushed through while loop until it reaches -1
Int array pushed through while loop until it reaches -1

Time:01-26

I have to take a single line of user input, and calculate the average of all the numbers until it reaches -1 using a while loop. An example of user input could be something like 2 -1 6 which is why I've done it this way. I've figured out how to split this into an int array, but I can't figure out how to do the while loop portion.

System.out.println("user input")
String user = scan.nextLine();
String[] string = user.split(" ");
int[] numbers = new int[string.length];
for(int i = 0;i < string.length;i  ) {
    numbers[i] = Integer.parseInt(string[i]);
}
while ( > -1){
            
}

CodePudding user response:

Class java.util.Scanner has methods hasNextInt and nextInt.

import java.util.Scanner;

public class Averages {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter series of integers on single line separated by spaces.");
        System.out.println("For example: 2 -1 6");
        int sum = 0;
        int count = 0;
        while (scan.hasNextInt()) {
            int num = scan.nextInt();
            if (num == -1) {
                break;
            }
            sum  = num;
            count  ;
        }
        if (count > 0) {
            double average = sum / (double) count;
            System.out.println("Average: "   average);
        }
        else {
            System.out.println("Invalid input.");
        }
    }
}

Note that you need to cast count to a double when calculating the average otherwise integer division will be performed and that will not give the correct average.

CodePudding user response:

System.out.println("user input")
String user = scan.nextLine();
boolean found = false;
Double average = 0;


String[] string = user.split(" ");
int[] numbers = new int[string.length];
for(int i = 0;i < string.length;i  ) {
    numbers[i] = Integer.parseInt(string[i]);
}

while (found == false){
      if(numbers[i] == - 1){
        average = average/i;
        found = true;
      }
      else{
            average = (Double) average   (Double) numbers[i];
          }            
}

CodePudding user response:

I am assuming you mean, when user input number is -1. we should take average of all number before -1. that is was I am doing here.

System.out.println("user input")
String user = scan.nextLine();
int totalSum = 0;
double avg = 0;

String[] string = user.split(" ");
int[] numbers = new int[string.length];
for(int i = 0;i < string.length;i  ) {
    numbers[i] = Integer.parseInt(string[i]);
    if(numbers[i]==-1){
        avg = (double)totalSum / i;
        break;
    }
    totalSum  = numbers[i]; 
}

With only while loop

System.out.println("user input");
String user = scan.nextLine();
int totalSum = 0;
double avg = 0;

String[] string = user.split(" ");
int[] numbers = new int[string.length];
int i = 0;
numbers[i] = Integer.parseInt(string[i]);
while(numbers[i]!=-1) {
    totalSum  = numbers[i];
    i  ;
    numbers[i] = Integer.parseInt(string[i]); 
}
avg = (double)totalSum / i;
  •  Tags:  
  • Related