what java code for output repeated continuously by requesting the user to input data&number?

Dani AI

Generated

A short, practical answer that fits the suggestions from @javaAddact and addresses 's question about the trigger. The common approaches are: a sentinel value (type "exit"), a menu choice that includes an Exit option, or a fixed number of sessions. The example below reads a string and a repeat count repeatedly until the user types "exit". Input parsing and simple validation are included.

import java.util.Scanner;

public class RepeatInput {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            while (true) {
                System.out.print("Enter text (type 'exit' to quit): ");
                String text = sc.nextLine();
                if (text == null) break;
                text = text.trim();
                if ("exit".equalsIgnoreCase(text)) break;

                System.out.print("Enter repeat count (positive integer): ");
                String countStr = sc.nextLine().trim();
                int count;
                try {
                    count = Integer.parseInt(countStr);
                    if (count <= 0) {
                        System.out.println("Count must be positive. Try again.");
                        continue;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Invalid number. Try again.");
                    continue;
                }

                int cap = Math.min(count, 100000); // safety cap
                for (int i = 0; i < cap; i++) {
                    System.out.println(text);
                }
                if (count > cap) {
                    System.out.println("Output limited to " + cap + " lines.");
                }
            }
        }
    }
}

Notes and troubleshooting: avoid mixing nextInt() and nextLine() (that often causes a skipped prompt). The sentinel approach used here keeps input handling simple. For other needs, replace the sentinel with a numeric sentinel (0), a menu choice, or a fixed-session for-loop. Be cautious about very large counts—either validate or cap them to prevent accidental floods. Closing a Scanner on System.in will close the stream; that is fine for small command-line tools but should be avoided inside libraries or larger apps.

Recommended Answers

All 3 Replies

huh? please be more specific and provide the code you already have

Probably he wants a menu with a while loop where the user can enter from the keyboard a number and a date several times

i figured as much, but without context we really can't tell him how he should do it anyway, granted the while loop is generally the same for that kind of think, but what does he want the trigger to be?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.