Java need help reading a line from a file then parse through that line to create Term objects that are put into a list that
Java need help reading a line from a file then parse through that line to create Term objects that are put into a list that are then added to an ArrayList for ObjectLists
Heres my code and input file: A term is two numbers example: 1 1 is a term next term is 22....so on the first line becomes a polynomial and the subsequent lines also become polynomials
1 1 2 2 3 3 4 4 8 -4 3 -2 1 1 6 3 4 2 -1 1 3 0
import java.io.*; import java.lang.Math; import java.util.ArrayList; import java.util.Scanner;
/** * Write a description of class Polynomial here. * * @author Josh Mumford * @version 11/14/2017 */ public class Polynomial { PrintWriter pw; ArrayList listOfPolys;
/** * Creates a Random number Object and ObjectList */ public Polynomial(PrintWriter pw) { this.pw = pw; listOfPolys = new ArrayList<>(); }
/** * getTerms() - creates term objects from file and creates polynomial lists that are housed * within an ArrayList object */ public void getTerms() throws IOException { Scanner scan = new Scanner(new File("polynomials.txt")); int count = 0; while(scan.hasNextLine()) { String line = scan.nextLine(); ObjectList list = new ObjectList(); for(int i = 0; i < line.length(); i++) { if(count != 2) { int coeff = Integer.parseInt(String.valueOf(line.charAt(i))); ++count; } else { int exp = Integer.parseInt(String.valueOf(line.charAt(i))); Term newTerm = new Term(coeff, exp); list.addLast(newTerm); } } listOfPolys.add(list); } scan.close(); } }
import java.io.*; import java.util.Scanner;
/** * Term() - has methods to create a term object including set and get methods for a Term * * @author Josh Mumford * @version 11/03/2017 */ public class Term { private int coeff; private int exp;
/** * Creates a term with a coeffient and an exponent */ public Term(int coeff, int exp) { this.coeff = coeff; this.exp = exp; }
/** * setCoeff() - sets the coefficient of a term * * @param c parameter for the coefficient argument in a term */ public void setCoeff(int c) { this.coeff = c; } /** * getCoeff() - gets the coefficient in a term * * @return int returns an integer for the coefficient */ public int getCoeff() { return this.coeff; } /** * setExp() - sets the exponent in a term * * @param e parameter for the exponent argument in a term */ public void setExp(int e) { this.exp = e; } /** * getExp() - gets the exponent in a term * * @return int returns an integer for the exponent */ public int getExp() { return this.exp; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started