0

In this assignment I have to store a integer string input, "1234", into a integer array and the program must output the array reversely, the sum and the average. It must continue to prompt the user to enter a string unless they enter "end".

Everything works its just the program prints out -14,4,3,2,1,-14, and I dont know how to fix that issue also I dont know what value to enter into the while loop to end the program. Any help would be appreciated

import java.util.*;

public class Assign1
{
public static void main(String[] args)
{
    int sum=0;
    String input;

    Scanner scan = new Scanner(System.in);

do{
    System.out.println("Enter in a string of integers: ");
    input = scan.nextLine();

    //makes the size of the array according to the length of the user input
    int[] intArr =  new int[input.length()];

    //loop to store integers in array
    for(int i=0; i<input.length(); i++)
    {
        intArr[i]= input.charAt(i)-'0';

    }

    //loop to calculate sum of elements in array
    for(int i : intArr)
    {
        sum=sum+i;
    }
    System.out.println("Sum of array elements is: " + sum);


    //calcluates the average of elements in array
    double average = sum/input.length();
    System.out.println("Average value of array elements is: " + average);


    printReversed(intArr);
  }
  while(input!=" ");

}
public static void printReversed(int[] values)
{

    //loop that prints out elements in reverse order
    for(int i= values.length-1; i>=0; i--)
    {
        System.out.println(values[i]);
    }



}

}

0 Answers0