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

The Archive Base Latest Questions

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

EDIT: I am trying to add elements read from a txt document line by

  • 0

EDIT:
I am trying to add elements read from a txt document line by line into an array list then convert that array list into an array. Although I am getting errors with my code. It doesnt like the int[] a = lines.toArray(new int[lines.size()]);.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class insertionSort {

public static void main(String[] args) {
    List<Integer> lines = new ArrayList<Integer>();
    File file = new File("10_Random.txt");

    try {

        Scanner sc = new Scanner(file);
        //int line = null;

        while (sc.hasNextLine()) {
            int i = sc.nextInt();
            lines.add(i);
            //System.out.println(i);
        }
        sc.close();
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    int[] a = lines.toArray(new int[lines.size()]);
}
}

Edit2: Thanks chaitanya10! all fixed.

  • 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:17:19+00:00Added an answer on June 13, 2026 at 8:17 pm
    int line= null; is wrong,
    

    “null is a special literal that can be of any object reference type”.you cant assign null to primitive variables in java like (int, byte, float…). null can only be assigned to objects . remember thatnullis the default vale forobjects` when you don’t initialize them.

    if you wanna access int as an object use Integer.

    Integer line= null;//nowthis would compile
    

    and to convert an list onto array do this.

    List.toArray(T[] t) method returns an Object.
    do like below.

    Integer[] array = lines.toArray(new Integer[lines.size()])
    

    and also your List accepts int[] array and you are tryig to add an int into the list .

    change your List declaration like this

    List<Integer> lines = new ArrayLis<Integer>();
    

    To print the elements in the array you have to iterate over it

        for(int i=0; i<a.length;i++){
        system.out.println(a[i])
    }
    

    you seem to be a beginner in java. strongly recommend you tohereread about java basic

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

Sidebar

Related Questions

i`m trying to make an NSMutableArray From NSUserDefaults so i can add/delete and edit
Am trying to add a link besides the Edit | Delete links in wordpress
I'm trying to add an edit function to my web app but I'm having
I am trying to add a simple form to allow my users to edit
I am trying to manipulate SharePoint's default calendar to add, edit and delete items
When trying Edit Table Data in MySQL Workbench 5.2.37, its in read only mode.
Edit: I'm trying to check the user input against a array of allowed inputs
I am trying to dynamically add some content to a list of checkboxes in
I have an unidimensional array that can have more the 50 elements and I'd
I'm trying to display two <div> elements on the same line, but in the

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.