Question: Have a program that will compile but has some run time errors involving number formating. I was hoping you could help me out with this.

Have a program that will compile but has some run time errors involving number formating. I was hoping you could help me out with this. its involving the section with "parseInt". The section is at the bottom in the Main class

import java.util.Scanner    // Polynomial class   public class Polynomial {       private Term head;         public Polynomial() {           head = null;       }         public Term getHead() {           return head;       }         // Function to insert a new node in the list       public void insertNode(int coeff, int exp)       {           Term newNode = new Term(coeff, exp);           // If list is empty           if (head == null)               head = newNode;           else {               Term temp = head;               // traverse the list till the end               while (temp.next != null)                   temp = temp.next;               // add the node to the end               temp.next = newNode;           }       }         // Function to sort the list       public void sortList()       {           Term temp1 = head;           Term temp2;           if (head == null)               return;           while (temp1.next != null) {               temp2 = temp1.next;               while (temp2 != null) {                   // checking for descending order                   if (temp1.exp < temp2.exp) {                       int c = temp1.coeff;                       int e = temp1.exp;                       temp1.coeff = temp2.coeff;                       temp1.exp = temp2.exp;                       temp2.coeff = c;                       temp2.exp = e;                   }                   temp2 = temp2.next;               }               temp1 = temp1.next;           }       }         // Function to evaluate the polynomial       public int evaluate(int x)       {           int sum = 0;           Term temp = head;           while (temp != null) {               sum += temp.coeff * Math.pow(x, temp.exp);               temp = temp.next;           }           return sum;       }         // Function to print the polynomial       public void printPolynomial()       {           Term temp = head;           while (temp != null) {               if (temp.coeff > 0)                   System.out.print("+");               if (temp.coeff != 0) {                   System.out.print(temp.coeff);                   if (temp.exp > 0)                       System.out.print("x^" + temp.exp);               }               temp = temp.next;           }       }   }       // Term class   public class Term {       public int coeff;       public int exp;       public Term next;         public Term(int c, int e)       {           coeff = c;           exp = e;           next = null;       }   }     // Main class   public class Main {      public static void main(String[] args) {          // creating object of the Polynomial class          Polynomial poly = new Polynomial();                      // read input from file              Scanner scanner = new Scanner("lab2.txt");              while (scanner.hasNextLine()) {                  String line = scanner.nextLine();                  String[] values = line.split(" ");                  int coeff = Integer.parseInt(values[0]);                  int exp = Integer.parseInt(values[1]);                  poly.insertNode(coeff, exp);              }              scanner.close();                    // sorting the list          poly.sortList();          // print the polynomial          poly.printPolynomial();          int x = 3;          // evaluate the polynomial          int result = poly.evaluate(x);          System.out.println(" = " + result + " FOR X = " + x);      }  }   

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The issue in the provided code lies in the Main class section where it reads input from a file Heres a breakdown of the problem and the solution Probl... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (2 attachments)

PDF file Icon

663dc38c682ba_962361.pdf

180 KBs PDF File

Word file Icon

663dc38c682ba_962361.docx

120 KBs Word File

Students Have Also Explored These Related Operating System Questions!