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

  • Home
  • SEARCH
  • 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 8783221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:46:27+00:00 2026-06-13T20:46:27+00:00

I am having troubles with java. I need to scan numbers (example: 1 3

  • 0

I am having troubles with java. I need to scan numbers (example: 1 3 4 2 5 6 7) and put them into array. The problem is I don’t know how long it will be. Is there a command that I can use to determine the length of the numbers putted in scanner?

  • 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-13T20:46:28+00:00Added an answer on June 13, 2026 at 8:46 pm

    If you don’t want to use ArrayList, as a workaround, I advice to read the entire line and then split to get the length and individual values:

       String line = scanner.nextLine();
       String[] values = line.split(" ");
       int[] intValues = new int[values.length];
       int indx = 0;
       for(String value:values){
         intValues[indx++] = Integer.parseInt(value);
       }
    

    EDIT: Second approach:

       List<Integer> numList = new ArrayList<Integer>();
       int number = 0;
       while (number != -1) {
          System.out.println("Enter a positive integer value (or -1 to stop): ");
          number = Integer.parseInt(in.nextLine());
          if (number != -1){
            numList.add(number);
          }
       }
       in.close();
    
       Integer[] numArray = numList.toArray(new Integer[numList.size()]);
    

    EDIT2: Taking care of multiple numbers in the same line and terminating at empty line

    List<Integer> numList = new ArrayList<Integer>();
    while(true) {
       System.out.println("Enter a positive integer value (or -1 to stop): ");
       String line = in.nextLine();
       if(line.length() <1){
          break;
       }
       String [] numbers = line.split(" ");
       for(String num: numbers){
              numList.add(Integer.parseInt(num));
       }
     }
     in.close();
    
     Integer[] numArray = numList.toArray(new Integer[numList.size()]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To make a long story short, I'm having trouble getting a couple of Java
I need an IntervalTree or RangeTree implementation in Java, and am having trouble finding
I'm having some troubles pausing and resuming a method. (Using Java in Eclipse). Basically
I just started learning Java and I'm having trouble formatting string. In the problem
I'm having trouble converting objects into JSONObject (org.json.JSONObject) in java. I have an object
I making web games in java and having troubles each time when getting to
I am having troubles migrating from OC4J 10.1.2.3 to 10.1.3.1.4. The problem is for
I am having trouble running a Java program with Windows Powershell 2.0. Any help
I'm having trouble transitioning to Java from C/C++ for my Telnet interface to some
I am having trouble understanding the following article: http://www.ibm.com/developerworks/java/library/j-jtp01255.html Under, Generics are not covariant

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.