0

I'm trying to code an array that will make user print in 125 digits and then add the sum of it, this works but im trying to make the message like:

Please enter digit : 1
please enter digit : 2
Please enter digit : 3
...

Do i have to make a new int for this to work?

this is my current code

import java.util.Scanner;
public class jasam {
    public static void main (String[] args) {
        Scanner scan = new Scanner(System.in);
int[] array = new int [125];
int sum = 0;
int counter = 0;

while(sum < 125){
    System.out.println("Please enter digit:" );
    array[counter]=scan.nextInt();

    sum += array[counter];
3
Contributors
5
Replies
20
Views
3 Years
Discussion Span
Last Post by Heanre
1

That's OK so far... except do you really want to stop taking input when the sum reaches 125?

ps System.out.print is just like System.out.println except it doesn't go to a new line at the end, so it would look more like the sample input in your post

Edited by JamesCherrill

0

Yes I want it to end by 125 so do you know how i make it look like the sample?

1

does this work for you? this program also traps invalid input(s),
and display the contents of array...if you don't need to display the items of the array, i bet it's ok if you don't put the numbers in the array and just simply add up everything...

/*
*@author:Giselle Jane Noynay
*/
import java.util.Scanner;
public class jasam
{
   public static void main(String[] args)
   {
      int counter=0;
      int sum=0;  
      Scanner scan=new Scanner(System.in);
      int arr[]=new int[125];
      while(counter<125)
      {
            try
            {  

               System.out.print("Please enter integer number "+ (counter + 1)+ ": ");
               arr[counter]=scan.nextInt();
               sum+=arr[counter];
               ++counter;
            }
            catch(Exception e)
            {
               System.out.println("Input integers only.");
               scan.nextLine();//FLUSHES INVALID INPUT

            }


      }
      //TO DISPLAY ITEMS IN ARRAY
      for(int i=0;i<arr.length;i++)
      {
         System.out.println(arr[i]);
      }
      System.out.println("Sum is: " + sum);
   }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.