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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:50:41+00:00 2026-06-02T20:50:41+00:00

So a part of my homework is to get a string of integer numbers

  • 0

So a part of my homework is to get a string of integer numbers and put each one of them in a list, this is what I have done:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    String N;
    int count;        
    LinkedList<Integer> booksList = new LinkedList<>();


    System.out.printf("Give: ");
    N = input.nextLine();

    String[] arr = N.split(" ");        

    for (count = 0; count < arr.length - 2; count++) {            
        booksList.add(Integer.parseInt(arr[count + 2]));
    }

So what I’ve done is take the string, split it into an array and then take the items with the for loop and put them into a list. What I am confused with is that I see people using the add command with a “new” inside the parentheses like this:

 booksList.add(new Integer.parseInt(arr[count + 2]));

This is confusing me and I’m not totally sure if I’m doing this the right way.
By the way, I take the array elements from count + 2 because I do not need the first two elements of the answer but I need to store the first two integers in two separate variables.

Also I’ve seen some people write this:

List<Integer> booksList = new LinkedList<>();

Is there any difference from that and what I’ve written?

  • 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-02T20:50:43+00:00Added an answer on June 2, 2026 at 8:50 pm

    The construct

    List<Integer> booksList = new LinkedList<>();
    

    is often seen for mainly 2 reasons:

    • a) You aren’t sure what the best collection for your purpose is. There are many candidates: Arrays, Sets, Lists, Vector, Map and so on, but you have to use something specific. So on the right hand side it has to be concrete, but on the left hand side, you try to be as wide open as possible.

    Very often you just do iterate over the collection, use a for loop, search, sort, put into, take from, delete.

    If you find out, that a sibling of your first thought is better suited, you can later change the RHS, without need to rewrite the rest of your program, because, for example, most methods of LinkedList and ArrayList fullfill the same contract, defined in List.

    • b) You get mor compatible to the outside world, if you use the wider object. Of course, you can’t use something as vague as Object, since Object has no .add (E) Method.

    But for sorting, searching and so on List is well suited. Have a look at the documentation, and compare List, LinkedList and ArrayList. Try to replace the one with another in your code.

    Maybe two ‘sublist (int fromIndex, int toIndex)’ methods, in Linked~ and ArrayList are defined in the base class, and behave identically.

    But maybe they are specialized, and implement subList on their own. Here we have a Interface <- Class relationship, so we know, that there is no implementation in the Interface, it is completly abstract. But in the case of (Abstract) base classes <- derived classes the pattern is the same.
    Derived/implementing classes share the same interface, ‘public List sublist (int fromIndex, int toIndex)’. You expect them to produce the same result, but in different ways – maybe.

    Only if you need to call a method which is only present in, say LinkedList, you either declare your bookList as LinkedList up front, or you cast it for that purpose:

    List<Integer> books = new LinkedList<>();
    // ...
    LinkedList <Integer> booksLiLi = (LinkedList <Integer>) books <> ();
    // do something with booksLiLi which isn't defined on the List interface.
    

    Sidenote: Your code can be simplified with the modern (7 years old) foreach-loop:

    Scanner input = new Scanner (System.in);
    LinkedList<Integer> booksList = new LinkedList<>();
    System.out.printf ("Give: ");
    String N = input.nextLine ();
    String[] arr = answer.split (" ");        
    
    for (String num: arr) {            
        booksList.add (Integer.parseInt (num));
    }
    bookList.remove ();
    bookList.remove ();
    

    But if you declare a Scanner, you can take Integers from the Scanner directly:

    Scanner input = new Scanner (System.in);
    LinkedList<Integer> booksList = new LinkedList<>();
    System.out.printf ("Give: ");       
    
    while (input.hasNextInt ()) {            
        booksList.add (input.nextInt ());
    }
    bookList.remove ();
    bookList.remove ();
    

    In reaction to the comments, here is a third code: Reading from System.in a line, and then creating a Scanner on that that line:

        String line = input.nextLine (); 
        Scanner valueLine = new Scanner (line);
        List<Integer> books = new LinkedList<>();
    
        while (valueLine.hasNextInt ()) {            
            books.add (valueLine.nextInt ());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(this is indirectly a part of a much larger homework assignment) I have something
I am a student, this is part of my homework. I have to Update
As part of my homework I have the following code: ArrayList <Integer> marks =
This is part of a homework assignment. What we have to do is write
PART OF A HOMEWORK PROBLEM I have a list of objects, and my goal
I'm almost done with all my homework, but the last part I have to
This is part of a homework assignment. I've got several questions asking find the
I am trying to learn TDD and this is part of my homework I
I'm doing a homework in MARS simulator (Assembly) and I'm stuck on one part.
This is homework...I'm not asking for answers, I just have a bug I'm not

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.