Reading a text file and converting it to polynomials

I currently have a text file:

3 5 6 9 3 4 6 7 2 3 5 7 2 5 3 

When reading in java, the file should display as 3x ^ 5 + 6x ^ 9. The second line will be read as 4x ^ 4 + 6x ^ 7 + 2. It is impossible to get my program to display this, because I do not know how to convert these numbers to this form . Currently, I only get numbers with spaces between them when I run the program.

Here is what I tried:

 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 numbers as polynomials, and then ultimately want to perform operations with them. Can anybody help me?

+5
source share
1 answer

It seems that polynomials have values ​​that appear in pairs, i.e. 5 4 become 5x^4 . This means that you need to keep track of whether it is the first of the pair, the second pair or not a member of the pair. In the first case, you just need to print the value, but the second you need to have the value "x ^" +.

You can do the following in your constructor for the polyomial class:

  • Create String polynomial = "" .

  • Scroll the line traversed while tracking boolean isOnSecond .

  • If !isOnSecond , add a value to read and set isOnSecond to true.

  • Else isOnSecond == true add "x^" + value and set isOnSecond to false.

  • Check if the line has a different value, and if it adds a "+" and saves the loop, otherwise it does nothing, because the line is completed.

This will give you lines that look like the result you want.


Sample code inside 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 } } 
+1
source

All Articles