Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8946831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:36:00+00:00 2026-06-15T12:36:00+00:00

The program is supposed to ask the user for a number, and loops until

  • 0

The program is supposed to ask the user for a number, and loops until the input is not a number (repeats if there is a number input).
My code so far:

import java.util.*;

public class MataVarden
{
    public static void main(String[] args) throws Exception
    {   
        Scanner sc = new Scanner(System.in);    
        ArrayList<Integer> Values = new ArrayList<Integer>(); 

        System.out.print("Input a number: ");
        Values.add(sc.nextInt()); 

        Collections.sort(Values);
        System.out.println("Values sorted: " + Values);
    }
}

What do you need to do in order to break an input if a non-number is entered?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:36:01+00:00Added an answer on June 15, 2026 at 12:36 pm

    Use a while loop for these case.

    System.out.println("Input a number");
    String line = "";
    while (sc.hasNextLine() && !(line = sc.nextLine()).equals("")) {
        try {
            int val = Integer.parseInt(line);
            values.add(val);
        } catch (NumberFormatException e) {
            break;
        }
    
        System.out.println("Input a number");
    }
    

    Ok now, here’s an explanation of what’s happening there: –

    • sc.hasNextLine() checks whether there is an input to read or not. If yes then proceed to the next test
    • !(line = sc.nextLine()).equals("") checks whether the next input is an empty string or not. If it’s an empty string, the condition fails, and loop ends.
    • Since we are reading the input using nextLine() method, we would have to parse it to integer using Integer.parseInt(line);
    • We need to enclose it in a try-catch block to handle input like "abc", which will not be parsed to an integer, and will throw an exception, in which case, we break out of the while loop from the catch block.
    • Note that I haven’t used sc.nextInt() because it does not read the newline token from the input. So, there would not be any way to know when to terminate the loop.

    As a side note, you should always follow Coding conventions in your code. Your variable name should start with a lowercase letter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently making a program which is supposed to prompt the user to
I am writing a program in C++ which is supposed to take an input
I would like to ask if there existed a program, which were intended to
I am using the following code: while (invalidInput) { // ask the user to
My program is supposed to tally up bottles from 4 rooms. When the user
I need to write a program, that will ask a user to enter a
I'm working on a homework assignment for Java, where a program is supposed to
I am supposed to use a for loop for use input -Not sure why
Let me ask whether you can make a C# program which can not access
The program is supposed to take a file, say data.dat, filled with a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.