user3369680
user3369680

Reputation:

Need Java print statement after loop

I am working on the current program and I need the * print statement to print all at once at the end. However, it keeps printing after each number is entered. I am new to java and any assistance would be appreciated.

import java.util.Scanner;

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

        int counter = 0;
        int value = 0;

        // counter loop for 5 inputs
        for (counter = 0; counter < 5; counter++){
            System.out.print("Input an integer between 1 and 30: ");
            value = input.nextInt();

            while(value < 1 || value > 30){
                System.out.print("Input an integer between 1 and 30: ");
                value = input.nextInt();
            }
            // print the asterisks based on value
            for (int i = 0; i < value; i++){
                System.out.print("*");

                }
                System.out.println();
        }
        System.exit (0);
    }
}

Current Output:

Input an integer between 1 and 30: 25
*************************
Input an integer between 1 and 30: 22
**********************
Input an integer between 1 and 30: 1
*
Input an integer between 1 and 30: 12
************
Input an integer between 1 and 30: 10
**********

Process finished with exit code 0.

Expected Output:

Input an integer between 1 and 30: 25
Input an integer between 1 and 30: 22
Input an integer between 1 and 30: 1
Input an integer between 1 and 30: 12
Input an integer between 1 and 30: 10


*************************
**********************
*
************
**********

Process finished with exit code 0.

Upvotes: 1

Views: 4150

Answers (4)

u3l
u3l

Reputation: 3412

Firstly, like the above answer stated, place the following for loop outside of the other loop:

for (int i = 0; i < value; i++){
     System.out.print("*");
}

rather than inside, as have you done.

Secondly, I do not see the point of having this:

System.out.print("Input an integer between 1 and 30: ");
value = input.nextInt();

while(value < 1 || value > 30){
       System.out.print("Input an integer between 1 and 30: ");
       value = input.nextInt();
}

Taking your input twice seems to be redundant. Delete the first two statements, and instead use do while:

do{
       System.out.print("Input an integer between 1 and 30: ");
       value = input.nextInt();
}while(value < 1 || value > 30);

I believe you'll still get a logical error, however (i'm not sure what your program intends to do). You seem to want to print the total number of asterixis, so you'd either have to add the value to itself each time you take input, or you'll have to use a stringbuilder to append it within the loop.

Upvotes: 1

user3256147
user3256147

Reputation: 398

You can't print all the stars continuously when you have variable as integer.
U need to have arrays .. Because if at all u need to print all the * at the end,you need to have track of the count of stars in each line... If u use integer, by the time you enter the next line count, you will lose the previous line count...

Algorithm for your ques is:

1.declare an array

2.get the total no of lines to be displayed

3.get the count of total no of stars in each line ( and store this in array )

4.display the stars based on the count in this array.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201527

You need an array to store to your various counts, here is one such solution -

public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  // values holds room for 5 inputs
  int[] values = new int[5];

  for (int counter = 0; counter < values.length; counter++) {
    System.out.print("Input an integer between 1 and 30: ");
    System.out.flush();
    int value = input.nextInt(); // <-- declare value here.

    while (value < 1 || value > 30) {
      System.out.print("Input an integer between 1 and 30: ");
      System.out.flush();
      value = input.nextInt();
    }
    values[counter] = value; // <-- store it in the values array.
  }
  // Use the for each operator to iterate over the value(s)...
  for (int value : values) {
    // print the asterisks based on value
    for (int i = 0; i < value; i++) {
      System.out.print("*");
    }
    System.out.println();
  }
  System.out.flush();
  System.exit(0);
}

Another approach would be to store your output into a StringBuilder, like so -

public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  StringBuilder sb = new StringBuilder();
  for (int counter = 0; counter < 5; counter++) {
    System.out.print("Input an integer between 1 and 30: ");
    System.out.flush();
    int value = input.nextInt();

    while (value < 1 || value > 30) {
      System.out.print("Input an integer between 1 and 30: ");
      System.out.flush();
      value = input.nextInt();
    }
    for (int i = 0; i < value; i++) {
      sb.append("*");
    }
    sb.append("\n");
  }
  System.out.println(sb.toString());
  System.out.flush();
  System.exit(0);
}

Upvotes: 1

DeadChex
DeadChex

Reputation: 4659

Move the for loop with the * print to right before the exit

Upvotes: 1

Related Questions