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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:29:42+00:00 2026-06-08T11:29:42+00:00

How do i read an external text file, possibly using a Scanner and store

  • 0

How do i read an external text file, possibly using a Scanner and store 2 values from each line into an array.

I would want to store the node id which is the first value of each line and the parent number which is the last value in each line.

The text file contains what you see below

1       2,7,|0|BLACK|NULL
10      3,4,12,|3|BLACK|3
11      4,12,|4|BLACK|4
12      8,10,11,|3|BLACK|8
2       1,3,6,8,|1|BLACK|1
3       2,4,5,6,8,10,|2|BLACK|2
4       3,5,9,10,11,|3|BLACK|3
5       3,4,8,|3|BLACK|3
6       2,3,|2|BLACK|2
7       1,8,|1|BLACK|1
8       2,3,5,7,9,12,|2|BLACK|2
9       4,8,|3|BLACK|8
  • 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-08T11:29:44+00:00Added an answer on June 8, 2026 at 11:29 am

    Shonna, you can achieve what you want using a HashMap, a Scanner, and some simple string parsing. Here’s my whole class:

    import java.io.*;
    import java.util.*;
    
    public class nodes {
    
        private static HashMap<Integer, String> map = new HashMap<Integer, String>();
    
        public static void main(String[] args) {
            File file = new File("nodes.txt");
            Scanner scnr = null;
            try {
                scnr = new Scanner(file);
            } catch (FileNotFoundException e) {
    
            }
            while(scnr.hasNext()) {
                String line = scnr.nextLine();
                String[] getId = line.split("\\s+");
                int id = Integer.parseInt(getId[0]);
                int count = 0;
                int copy = 0;
                for(int i = 0; i < line.length(); i++) {
                    if(line.charAt(i) == '|')
                        count++;
                    if(count == 3) {
                        copy = i;
                        break;
                    }
                }
                String parent = line.substring(copy + 1);
                map.put(id, parent);
                System.out.println(map);
            }
        }
    
    }
    

    What it does is it reads through each line of the file, first extracting the node id. It then cycles through each character in the line until it counts three |‘s, at which point we know the rest of the line will be the parent of the node. After this is done, it pairs the id with the parent in a HashMap.

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

Sidebar

Related Questions

I'm looking for a way to read from an external text file map.txt (allocated
I usually add some strings from a text file into a list or array
I need to read the nth line of a text file (e.g. textfile.findline(0) would
I read in a Byte Array generated from a function called from an external
I'm using SimpleXML and xpath to read elements from an external UTF-8 XHTML document.
Is there a way to read off from an [external] xml (an xml file
I am trying to read in a large text file (~2.5 MB) into my
I wish to use an external Text/CSV file to read data and run an
I'm quite new to SQLite databases and need help using an external text file
Currently am trying to read and write mp3 tags without using any external modules

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.