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 7887389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:34:01+00:00 2026-06-03T05:34:01+00:00

Essentially, I’m trying to store user input in a multi dimensional array using for

  • 0

Essentially, I’m trying to store user input in a multi dimensional array using for loops. If the user types in something which is not expected/wanted (e.g. less than 0), a warning message will be displayed and the loop should ideally ‘wait’ till it receives the next valid integer.

Currently, my code, as shown below, works OK but I’m wondering if there are any better/more optimized ways of doing this.

for (int row = 0; row < array.length; row++) {
    for (int column = 0; column < array[row].length; column++) {
        int number = input.nextInt();
        if((input.nextInt() >= 0) {
            array[row][column] = number;
        } else {
            System.out.println("Input must be > 0.");
            column--;
        }
}
  • 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-03T05:34:03+00:00Added an answer on June 3, 2026 at 5:34 am

    Use a do..while loop to wait until the user inputs something valid. This is cleaner than modifying a loop counter.

    for (int row = 0; row < array.length; row++) {
            for (int column = 0; column < array[row].length; column++) {
                bool hasEnteredValidInput = false;
                do
                {
                   int number = input.nextInt();
                   if(number  >= 0) {
                      array[row][column] = number;
                      hasEnteredValidInput = true
                   } else {
                      System.out.println("Input must be > 0.");
                   }
                } while (!hasEnteredValidInput);
            }
        }
    

    Even better would be to extract the reading code into it’s own function:

    for (int row = 0; row < array.length; row++) {
            for (int column = 0; column < array[row].length; column++) {
                 array[row][column] = readValidInputFromUser();   
            }
     }
    
     public string readValidInputFromUser()
     {
        while(true)
        {
               int number = input.nextInt();
               if(number >= 0) {
                  return number;
               } else {
                  System.out.println("Input must be > 0.");
               }
        }
     }
    

    This version makes it very clear what you’re doing.

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

Sidebar

Related Questions

Essentially I've got a table holding user data, all of which is AES encrypted
Essentially I have a method of a class called killProgram, which is intended to
Essentially I have an NSMutableArray which contains several NSDictionaries . I wish to sort
Essentially, what I'm trying to do is to check the last access time of
Essentially what I want is for the app, once the user has allowed access
Essentially Im trying to get many many java clients connect to a socket on
Essentially I'm trying to calculate the bisector line between two points. I've got two
Essentially the title of this question explains the essense of what I am trying
Essentially, what I'm trying to do is to load/draw a map from a Tiled
Essentially, I'm grabbing source code which contains entities such as &#38; and &#62; which

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.