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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:06:41+00:00 2026-05-29T08:06:41+00:00

So the app reads from an external file a bunch of strings, each on

  • 0

So the app reads from an external file a bunch of strings, each on a separate line.

For example:

and
cake
here

It is not arranged in any particular order. I need to read these letters and put them into linked list and finally sort them.

I need help on doing that:

Here is the current code:

import java.util.*;
import java.io.*;
public class LinkedList
{
  static File dataInpt;
  static Scanner inFile;
  public static void main(String[] args) throws IOException
  {
    dataInpt=new File("C:\\lldata.txt");
    inFile=new Scanner(dataInpt);
    Node first = insertInOrder();
    printList(first);
  }
  public static Node getNode(Object element)
  {
    Node temp=new Node();
    temp.value=element;
    temp.next=null;
    return temp;
  }
  public static void printList(Node head)
  {
    Node ptr; //not pointing anywhere
    for(ptr=head;ptr!=null;ptr=ptr.next)
      System.out.println(ptr.value);
    System.out.println();
  }
  public static Node insertInOrder()
  {
    Node first=getNode(inFile.next());
    Node current=first,previous=null;
    Node last=first;
    int count=0;
    while (inFile.hasNext())
    {
      if (previous!=null
          && ((String)current.value).compareTo((String)previous.value) > 0)
      {
        last.next=previous;
        previous=last;
      }
      if (previous!=null
          && ((String)current.value).compareTo((String)previous.value) < 0)
      {
        current.next=last;
        last=current;
      }
      previous=current;
      current=getNode(inFile.next());
    }
    return last;
  }
}

But that gives an infinite loop with “Cat”.

Here is the data file:

Lol
Cake
Gel
Hi
Gee
Age
Rage
Tim
Where
And
Kite
Jam
Nickel
Cat
Ran
Jug
Here
  • 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-05-29T08:06:42+00:00Added an answer on May 29, 2026 at 8:06 am

    Okay, self-study. Split the reading and inserting. Though old and new code both have 14 lines of code,
    it makes it more intelligable.

    public static Node insertInOrder() {
        Node first = null;
        while (inFile.hasNext()) {
            String value = inFile.next().toString();
            first = insert(first, value);
        }
        return first;
    }
    
    /**
     * Insert in a sub-list, yielding a changed sub-list.
     * @param node the sub-list.
     * @param value
     * @return the new sub-list (the head node might have been changed).
     */
    private static Node insert(Node node, String value) {
        if (node == null) { // End of list
            return getNode(value);
        }
        int comparison = node.value.compareTo(value);
        if (comparison >= 0) { // Or > 0 for stable sort.
            Node newNode = getNode(value); // Insert in front.
            newNode.next = node;
            return newNode;
        }
        node.next = insert(node.next, value); // Insert in the rest.
        return node;
    }
    

    This uses recursion (nested “rerunning”), calling insert inside insert. This works like a loop, or work delegation to a clone, or like a mathematical inductive proof.


    Iterative alternative

    also simplified a bit.

    private static void Node insert(Node list, String value) {
        Node node = list;
        Node previous = null;
        for (;;) {
            if (node == null || node.value.compareTo(value) >= 0) {
                Node newNode = getNode(value);
                newNode.next = node;
                if (previous == null)
                    list = newNode;
                else
                    previous.next = newNode;
                break;
            }
            // Insert in the rest:
            previous = node;
            node = node.next;
        }
        return list;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an iPhone app that reads data from an external XML file
I have successfully created an app that reads from a bundled .plist file and
I have a C# console app App1 that reads a row of data from
I am developing a BlackBerry app which needs to read from an xml file.
I have a method for my app to read a random line from a
i have csv files, java app and database, i read csv file from my
I have an app that needs to read a PDF file from the file
I'm using this code to read the connection string from my app.config file but
I got a external .net assembly including app.config which I need to call from
Current situation I'm executing an external application (by using Runtime.getRuntime().exec ... example here )

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.