I currently have a text file as follows:
3 5 6 9
3 4 6 7 2
3 5 7 2 5 3
The file when read into java should be displayed as 3x^5 + 6x^9. The second line would be read as 4x^4 + 6x^7 + 2. The cannot get my program to display this as I dont know how to convert these numbers into that form. I currently get just the numbers with spaces between them when i run the program.
Here is what I have attempted:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Driver {
public static void main(String[] args) {
try {
@SuppressWarnings("resource")
Scanner myfile = new Scanner(new File("poly.dat"));
Polynomial[] mypolynomial;
mypolynomial = new Polynomial[10];
int index = 0;
if (myfile.hasNext() == true) { //ignore this part
myfile.nextLine();
} else {
System.out.println("Error: File is empty");
return;
}
while (myfile.hasNextLine()) {
mypolynomial[index] = new Polynomial(myfile.nextLine());
index++;
}
String menu = "Please choose a Polynomial \n";
for (int i = 0; i < index; i++) {
menu = menu + i + " " + mypolynomial[i].getNumber() + "\n";
}
String choicemenu = "What do you want to do ? \n " + "A - Display a Polynomial \n "
+ "B - Add two Polynomial \n " + "C - Subtract two Polynoimal \n "
+ "D - Multiply two Polynomial \n ";
String action = JOptionPane.showInputDialog(choicemenu);
if (action.equals("A")) {
int choice = Integer.parseInt(JOptionPane.showInputDialog(menu));
JOptionPane.showMessageDialog(null, mypolynomial[choice]);
}
} catch (FileNotFoundException e) {
System.out.println(" OOOPS - something wrong - maybe the file name is wrong");
}
}
}
public class Polynomial { //Testing the program
String poly;
public Polynomial(String p)
{
poly = p;
}
public String getNumber() {
return poly;
}
public void setNumber(String p)
{
poly=p;
}
public String toString()
{
String result = "The Polynomial is " + poly;
return result;
}
}
I want to first display these digits as polynomials, then I eventually want to perform operations with them. Can anyone help me?
It appears that the polynomials have values that come in as pairs, i.e. 5 4
becomes 5x^4
. This means you have to keep track of whether it is the first of a pair, second of a pair, or is not a member of a pair. The first case you just need to print the value, but the second you need to have a "x^"+value.
You could do the following in your polynomial class constructor:
Create String polynomial = ""
.
Loop through the line passed in while keeping track of a boolean isOnSecond
.
If !isOnSecond
, append value read in and set isOnSecond
to true.
Else isOnSecond == true
append "x^" + value
and set isOnSecond
to false.
Check if string has another value, and if it does append a " + " and keep looping, else do nothing since the line is finished.
This will give you strings that look like the output you need.
Example code within your Polynomial
constructor:
public Polynomial(String p) {
// First step is to create a scanner of the String
// passed into the constructor.
Scanner scanner = new Scanner(p);
// Next step is to initialize the String poly
// to an empty String so we can append to it.
poly = "";
// Next we need to include a way of keeping track of
// whether the value we just read was a first or second
// member of a pair. We can do that with a boolean
// initialized to false since the first use will be
// when it is on the first of a pair.
boolean isOnSecond = false;
// Now we need to start looping through the values in
// p which are separated by white space. Scanner has
// a method for that, scanner.next().
while(scanner.hasNext()) {
String currentValue = scanner.next();
// Now is where the boolean comes into play.
if(isOnSecond) { // second of a pair
// the second of a pair needs to have "x^" before its value
poly = poly + "x^" + currentValue;
// Here we need to check if there is another value coming after
// and if there is append a " + " to poly
if(scanner.hasNext() {
poly = poly + " + ";
}
}
else { // !isOnSecond, so is first of a pair
// Only need to append the currentValue here
poly = poly + currentValue;
}
isOnSecond = !isOnSecond; // toggles isOnSecond
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With