How can I get the system output value after the whole loop is done?

Viewed 73

this is my first question in this community as you can see I'm a beginner and I have very little knowledge about java and coding in general. however, in my beginner practices, I came up with a little project challenge for myself. as you can see in the figure, the loop starts and it prints out the number that is given to it through the scanner. the problem with my attempt to this code is that it gives me the output value as soon as I press enter. what I want to do is an alternative of this code but I want the output values to be given after the whole loop is done all together.

figure

So, basically what I want is to make the program give me the input values together after the loop ends, instead of giving them separately after each number is put.

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        calc();    }

    public static int calc (){
        Scanner scan = new Scanner(System.in);
        int count = 1;
        int pass = 0;
        int notpass = 0;
        System.out.println("how many subjects do you have? ");
        boolean check = scan.hasNextInt();
        int maxless = scan.nextInt();

        if (check){

            while(count <= maxless ){
                System.out.println("Enter grade number " + count);
                int number = scan.nextInt();
                System.out.println("grade number" + count + " is " + number);
                if (number >= 50){
                    pass++;
                }else{
                    notpass++;
                }
                count++;
            }

            System.out.println("number of passed subjects = " + pass);
            System.out.println("number of failed subjects = " + notpass);

        }else{
            System.out.println("invalid value!");
        } return pass;
    }

}


2 Answers

Approach 1 :

You can use ArrayList from Collection Classes and store the result there and after the loop is completed, just print the array in a loop.

Example :

//Import class
import java.util.ArrayList;

//Instantiate object
ArrayList<String> output = new ArayList();

while(condition){
   output.add("Your data");
}

for(i = 0; i < condition; i++){
    System.out.println(output.get(i));
}

Approach 2 :

Use StringBuilder class and append the output to the string, after the loop is completed, print the string from stringbuilder object.

Example :

//import classes
import java.util.*;

//instantiate object
StringBuilder string = new StringBuilder();

while(condition){
    string.append("Your string/n");
}

System.out.print(string.toString());

Approach 3 : (As mentioned by Sarah)

Use arrays to store the result percentage or whatever and format it later in a loop. (Not a feasible approach if you want to store multiple values for the same student)

Example :

int studentMarks[] = new int[array_size];

int i = 0;
while(condition){
    studentMarks[i++] = marks;
}

for(int j = 0; j < i; j++)
    System.out.println("Marks : " + studentMarks[j]);

I think what you want to do is create an array of int numbers.

It would be something like this:

import java.util.Scanner;

public class Application {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int maxless = 5;
        int[] numbers = new int[maxless];

        int count = 0, pass = 0, notPass = 0;

        while(count < maxless){
            System.out.println("Enter grade number " + (count + 1) + ":");
            numbers[count] = scan.nextInt();
            if(numbers[count] >= 50){
                pass++;
            }
            else{
                notPass++;
            }
            count++;
        }

        for(int i=0; i<maxless; i++){
            System.out.println("Grade number " + (i + 1) + " is " + numbers[i]);
        }

    }

}

The output is the following:

Enter grade number 1:
90
Enter grade number 2:
76
Enter grade number 3:
54
Enter grade number 4:
67
Enter grade number 5:
43
Grade number 1 is 90
Grade number 2 is 76
Grade number 3 is 54
Grade number 4 is 67
Grade number 5 is 43

When dealing with arrays, just remember that the indexation begins at 0. You can read more about arrays here: http://www.dmc.fmph.uniba.sk/public_html/doc/Java/ch5.htm#:~:text=An%20array%20is%20a%20collection,types%20in%20a%20single%20array.

A tip: it's gonna be easier to help if you post the code on your question as a text, not an image, so we can copy it and try it on.

Related