Hello, I have to write a program that will solve polynomials by user input, my program compiles, but gets an exception error.
Here is my code:
import java.io.*;
import java.util.Scanner;
import java.util.LinkedList;
public class PolynomialList
{
private Node first = new Node(0, 0);
private Node last = first;
private static class Node
{
int co_eff;
int expo;
Node next;
Node(int co_eff, int expo)
{
this.co_eff = co_eff;
this.expo = expo;
}
}
private PolynomialList() { }
public PolynomialList(int co_eff, int expo)
{
last.next = new Node(co_eff, expo);
last = last.next;
}
public PolynomialList plus(PolynomialList b)
{
PolynomialList a = this;
PolynomialList c = new PolynomialList();
Node x = a.first.next;
Node y = b.first.next;
while (x != null || y != null)
{
Node t = null;
if (x == null) { t = new Node(y.co_eff, y.expo); y = y.next; }
else if (y == null) { t = new Node(x.co_eff, x.expo); x = x.next; }
else if (x.expo > y.expo) { t = new Node(x.co_eff, x.expo); x = x.next; }
else if (x.expo < y.expo) { t = new Node(y.co_eff, y.expo); y = y.next; }
else {
int co_eff = x.co_eff + y.co_eff;
int expo = x.expo;
x = x.next;
y = y.next;
if (co_eff == 0) continue;
t = new Node(co_eff, expo);
}
c.last.next = t;
c.last = c.last.next;
}
return c;
}
public PolynomialList times(PolynomialList b)
{
PolynomialList a = this;
PolynomialList c = new PolynomialList();
for (Node x = a.first.next; x!= null; x = x.next)
{
PolynomialList temp = new PolynomialList();
for (Node y = b.first.next; y!= null; y = y.next)
{
temp.last.next = new Node(x.co_eff * y.co_eff, x.expo + y.expo);
temp.last = temp.last.next;
}
c = c.plus(temp);
}
return c;
}
public String toString()
{
String s = "";
for (Node x = first.next; x != null; x = x.next)
{
if (x.co_eff > 0) s = s + " + " + x.co_eff + "x^" + x.expo;
else if (x.co_eff < 0) s = s + " - " + (-x.co_eff) + "x^" + x.expo;
}
return s;
}
public static void main(String[] args)
{
PolynomialList zero = new PolynomialList(0, 0);
Scanner sc = new Scanner(System.in);
System.out.println("Enter first coeffecient: ");
int i1 = sc.nextInt();
System.out.println("Enter first exponent: ");
int i2 = sc.nextInt();
System.out.println("Enter second coeffecient: ");
int i3 = sc.nextInt();
System.out.println("Enter second exponent: ");
int i4 = sc.nextInt();
System.out.println("Enter third coeffecient: ");
int i5 = sc.nextInt();
System.out.println("Enter third exponent: ");
int i6 = sc.nextInt();
System.out.println("Enter fourth coeffecient: ");
int i7 = sc.nextInt();
System.out.println("Enter fourth exponent: ");
int i8 = sc.nextInt();
PolynomialList p1 = new PolynomialList(i1, i2);
PolynomialList p2 = new PolynomialList(i3, i4);
PolynomialList p3 = new PolynomialList(i5, i6);
PolynomialList p4 = new PolynomialList(i7, i8);
PolynomialList p = p1.plus(p2).plus(p3).plus(p4);
System.out.println("Enter first coeffecient for 2nd polynomial: ");
int i9 = sc.nextInt();
System.out.println("Enter first exponent for 2nd polynomial: ");
int i10 = sc.nextInt();
System.out.println("Enter first coeffecient for 2nd polynomial: ");
int i11 = sc.nextInt();
System.out.println("Enter first exponent for 2nd polynomial: ");
int i12 = sc.nextInt();
PolynomialList q1 = new PolynomialList(i9, i10);
PolynomialList q2 = new PolynomialList(i11, i12);
PolynomialList q = q1.plus(q2);
PolynomialList r = p.plus(q);
PolynomialList s = p.times(q);
System.out.println("zero(x) = " + zero);
System.out.println("p(x) = " + p);
System.out.println("q(x) = " + q);
System.out.println("p(x) + q(x) = " + r);
System.out.println("p(x) * q(x) = " + s);
}
}
And I get this error:
Enter first coeffecient:
Exception in thread "main" [Finished in 0.9s with exit code 1]java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at PolynomialList.main(PolynomialList.java:100)
I'm sure there is a way to streamline the user input, because that's the issue, I just don't know how to approach it, or if my way works with a small tweak, thanks in advance.