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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:52:54+00:00 2026-05-30T02:52:54+00:00

I am using String Tokenizer and Linked Lists, and the Linked Lists are required

  • 0

I am using String Tokenizer and Linked Lists, and the Linked Lists are required for this assignment. There is an external file in there are numerous lines of polynomials (one per line). Using String Tokenizers and Linked List, I am running a while loop which captures two lines on each pass and adds them to linked lists. After the numbers have been loaded into linked lists, the goal is to add those polynomials together from their linked list and create a new linked list that contains that polynomial.

For example, first two lines in the file are this:

2x^4 -5x^3 +9x^2 -10

3x^4 -6x^3 +10x^2 -11


=
5x^4 -11x^3 +19x^2 -21

Here is my code:

public class PolynomialAddition
{
    static File dataInpt;
    static Scanner inFile;

    public static void main(String[] args) throws IOException
    {
      dataInpt=new File("C:\\llpoly.txt");
      inFile=new Scanner(dataInpt);
      StringTokenizer myTokens;
      String line,polyTerm;
      Node firstL=new Node();
      Node secondL=new Node();
      line=inFile.nextLine();
      myTokens=new StringTokenizer(line);
      polyTerm=myTokens.nextToken();
      firstL.value=polyTerm.substring(0,polyTerm.indexOf("x"));
      firstL.value2=polyTerm.substring(polyTerm.indexOf("^")+1);

    }
}

Here is my node class:

public class Node
{
  public Object value;
  public Object value2;
  public Node next;

  public Node()
  {
    value=null;
    value2=null;
    next=null;
  }
  public Node (Object value, Object value2, Node next)
  {
    this.value=value;
    this.value2=value2;
    this.next=next;
  }
}

The problem comes after this where some lines are not complete while the line they have to be added to is complete like -12x^8 +5x^2 -3 and 8x^3 +2x

The answer to this is supposed to be -12x^8 +8x^3 +5x^2 +2x -3

What can I do to solve this?

  • 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-30T02:52:56+00:00Added an answer on May 30, 2026 at 2:52 am

    Ok, after lenghty labour in the chat, this is what ‘we’ came up with. I realize this is just blurting the answer, to an extent.

    Even so, having a solid implementation in clean style Java 1.4 code could do a lot to help your understanding.

    Special attention has been given to printing the result in tabulated form, aligning terms of different operand in the columns of their respective exponent.

    Code

    There are two files:

    Node.java

    class Node {
        int factor;
        int exponent;
        Node next;
    
        public Node() {
            factor = 0;
            exponent = 0;
            next = null;
        }
    
        public Node(int factor, int exponent, Node next) {
            this.factor = factor;
            this.exponent = exponent;
            this.next = next;
        }
    
        public String toString() {
            return String.format("%+4dx^%d    ", new Integer[] { new Integer(factor), new Integer(exponent) }); 
        }
     }
    

    PolynomialAddition.java

    import java.io.*;
    import java.util.*;
    
    public class PolynomialAddition {
        static File dataInpt;
        static Scanner inFile;
    
        public static void main(String[] args) throws IOException {
            dataInpt = new File("/tmp/input.txt");
            inFile = new Scanner(dataInpt);
    
            while (inFile.hasNextLine()) {
                Node first = readPolynomial();
    //          printList(first);
    
                Node second = readPolynomial();
    //          printList(second);
    
                Node addition = addPolynomials(first, second);
    //          printList(addition);
    
                printTabulated(first, second, addition);
    
                System.out.println("\n");
            }
        }
    
        private static Node addPolynomials(Node first, Node second) {
            Node head = null, current = null;
            while (null!=first || null!=second)
            {
                boolean pickfirst = false;
                boolean haveBoth = (null!=first && null!=second);
    
                Node node;
                if (haveBoth && first.exponent == second.exponent)
                {
                    node = new Node(first.factor + second.factor, first.exponent, null);
                    first = first.next;
                    second = second.next;
                } else
                {
                    pickfirst = first!=null && 
                        ((second == null) || first.exponent > second.exponent);
    
                    if (pickfirst)
                    {
                        node = new Node(first.factor, first.exponent, null);
                        first = first.next;
                    } else
                    {
                        node = new Node(second.factor, second.exponent, null);
                        second = second.next;
                    }
                }
    
                if (current == null)
                {
                    head = node;
                    current = head;
                } else
                {
                    current.next = node;
                    current = node;
                }
            }
    
            return head;
        }
    
        private static void printTabulated(Node first, Node second, Node addition) {
            String line1="", line2="", barline="", line3="";
            while (addition != null)
            {
                String 
                     part1 = "           ", 
                     part2 = "           ", 
                     part3 = "           ";
    
                if (null!=first && first.exponent == addition.exponent) 
                {
                    part1 = first.toString();
                    first = first.next;
                } 
                if (null!=second && second.exponent == addition.exponent) 
                {
                    part2 = second.toString();
                    second = second.next;
                }
                part3 = addition.toString();
                addition = addition.next;
    
                line1 += part1;
                line2 += part2;
                barline += "-----------";
                line3 += part3;
            }
    
            System.out.println(line1);
            System.out.println(line2);
            System.out.println(barline);
            System.out.println(line3);
        }
    
        private static Node readPolynomial() {
            String line = inFile.nextLine();
            StringTokenizer myTokens = new StringTokenizer(line);
    
            Node head = null, previous = null;
            while (myTokens.hasMoreTokens()) {
                Node current = new Node();
                String term = myTokens.nextToken();
    
                if (term.startsWith("+"))
                    term = term.substring(1);
    
                current.factor = Integer.parseInt(
                        term.substring(0, term.indexOf("x")));
                current.exponent = Integer.parseInt(
                        term.substring(term.indexOf("^") + 1));
    
                if (previous == null)
                {
                    head = current;
                    previous = head;
                } else
                {
                    previous.next = current;
                    previous = current;
                }
            }
            return head;
        }
    
        private static void printList(Node head) {
            for (Node ptr = head; ptr != null; ptr = ptr.next)
                System.out.print(ptr);
            System.out.println();
        }
    }
    

    Sample Data:

    Input:

    2x^4 -5x^3 +9x^2 -10x^0 
     3x^4 -6x^3 +10x^2 -11x^0 
     -2x^1 +4x^0 
     2x^1 -4x^0 
     8x^5 +6x^4 +5x^2 -3x^0 
     -12x^8 +2x^7 +14x^5 
     1x^5 +7x^2 +8x^1 
     -5x^4 -7x^3 -4x^2 -3x^0 
     10x^5 -3x^3 +4x^2 -234x^1 -12x^0 
     -5x^5 -2x^3 +10x^0
    

    Output:

      +2x^4      -5x^3      +9x^2     -10x^0    
      +3x^4      -6x^3     +10x^2     -11x^0    
    --------------------------------------------
      +5x^4     -11x^3     +19x^2     -21x^0    
    
    
      -2x^1      +4x^0    
      +2x^1      -4x^0    
    ----------------------
      +0x^1      +0x^0    
    
    
                            +8x^5      +6x^4      +5x^2      -3x^0    
     -12x^8      +2x^7     +14x^5                                     
    ------------------------------------------------------------------
     -12x^8      +2x^7     +22x^5      +6x^4      +5x^2      -3x^0    
    
    
      +1x^5                            +7x^2      +8x^1               
                 -5x^4      -7x^3      -4x^2                 -3x^0    
    ------------------------------------------------------------------
      +1x^5      -5x^4      -7x^3      +3x^2      +8x^1      -3x^0    
    
    
     +10x^5      -3x^3      +4x^2    -234x^1     -12x^0    
      -5x^5      -2x^3                           +10x^0    
    -------------------------------------------------------
      +5x^5      -5x^3      +4x^2    -234x^1      -2x^0    
    

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

Sidebar

Related Questions

I am trying to read contents of a file using string tokenizer and store
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Are there any codes that allow for numerical formatting of data when using string.format?
AM using string tokenizer to delimit the string response by ^ 12/30/2011 12:00:00 AM^President^^^159^True^True^True^True^True^False^False^True^True^3/18/2011
I am using String Tokenizer in my program to separate strings. Delimiter I am
If a string has been processed using a Boost tokenizer is it possible to
I believe I am not using correctly String Tokenizer. Here is my code: buffer
If I want to copy the content of a text file using a string
I am trying to compile this .c code in windows using MinGW (gcc file.c
Using String.Format how can i ensure all numbers have commas after every 3 digits

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.