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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:41:11+00:00 2026-06-13T05:41:11+00:00

I need help understanding how to write a for loop that takes in a

  • 0

I need help understanding how to write a for loop that takes in a certain amount of integers (must be 1 through 10) and it stops taking in numbers once 0 is entered (0 will be the last number). My code so far is:

   import java.util.Scanner;
   public class countNum {

      public static void main(String[] args) {

        int[] array;

        Scanner input = new Scanner(System.in);
        System.out.println ("Enter in numbers (1-10) enter 0 when finished:");

        int x = input.nextInt();

        while (x != 0) {
          if (x > 2 && x < 10) {
          //Don't know what to put here to make array[] take in the values
          }
          else
          //Can I just put break? How do I get it to go back to the top of the while loop?
        }
      }   

     }

}

I don’t understand how to simultaneously initialize an array with a set length while having the Scanner read a certain amount of digits of that unknown length, until 0 is entered, and then the loop stops taking in input for the array.

Thanks for any help!

  • 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-13T05:41:13+00:00Added an answer on June 13, 2026 at 5:41 am

    Ok here’s the bit more detail: –

    • You need to use an ArrayList if you want a dynamically increasing array. You do it like this: –

      List<Integer> numbers = new ArrayList<Integer>();
      
    • Now, in your above code, you can put your number reading statement (nextInt) inside the while loop, since you want to read it regularly. And put a condition in while loop to check whether the entered number is an int or not: –

      int num = 0;
      while (scanner.hasNextInt()) {
          num = scanner.nextInt();
      }
      
    • Further, you can move on your own. Just check whether the number is 0 or not. If it is not 0, then add it to ArrayList: –

      numbers.add(num);
      
    • If its 0, break out of your while loop.

    • And you don’t need that x != 0 condition in your while loop, as you are already checking it inside the loop.

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

Sidebar

Related Questions

I need help understanding how to manage the API for an iPhone application that
I need help understanding how to write a permutation algorithm. (if this is even
I need help in understanding how to transfer a record through Indy TCP Server/Client.
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need help understanding whats going on in the function, especially the return statements.
I need help understanding how to demonize a process in Go. package main import
Kind of need help understanding what this code actually outputs. Does it out put
I'm fixing bugs for some application, and I need help understanding the following lines
OK, I need some help understanding the process behind Facebook's website integration process and
Oracle's PL/SQL is fairly new to me, so I need some help understanding if

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.