0

I am writing a program that will take input of polynomials. Needing to create a new ArrayList for each polynomial input, I am needing a way to name each list without knowing the amount of polynomial beforehand. If a file has 2 polynomials I will need to name 2 arrays, but if more polynomials I will need to name more arrays. Is there anyway to automatically name arrays or variables with the iteration of a loop. I cannot figure out how. Variable names such as : P1 , P2, P3 etc. as the number of Polynomials increases is what I am searching for. Each polynomial will be read in line by line. I have attached my code, Though it is nowhere near complete. I imagine I will need to move the PolyCalc creation into the while loop and create a new PolyCalc for each line of input. I am looking to add this feature to the while loop in the main method.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


public class PolyProcessor {
    static int polyNum = 0;
public static void main(String[] args) throws FileNotFoundException{


    File polyfile = new File("polyinput.txt");
    Scanner read = new Scanner(polyfile);
    while (read.hasNextLine()){
        PolyCalc c = new PolyCalc();
        String j = read.nextLine();
        c.add(j.split(";"));
        polyNum++;}


    }
    }


class PolyCalc{
    static int polyCount = 0;

    static ArrayList polynomials = new ArrayList();

     static void add(String[] strings){
         for(int i = 0; i<strings.length;i++){
           polynomials.add(strings[i]);
           polyCount++;}
     }
     static Object get(int i){
         return polynomials.get(i);

     }
}
Dave
  • 131
  • 2
  • 10
  • Why do you think these polynomials need names? – user2357112 Aug 30 '14 at 06:23
  • because I need to add or subtract them. The user will be asked to choose which polynomials they want to add. so if they add 5 polynomials from the file they can choose to add together poly 1 and poly 5, or poly 4 and poly 3. – Dave Aug 30 '14 at 06:27
  • 1
    That doesn't mean you need names. You have a list. That's all you need. – user2357112 Aug 30 '14 at 06:36
  • `HashMap` would be your best bet. `HashMap` where the string is what you are calling variable – Ker p pag Aug 30 '14 at 06:38
  • Each line of the input file will look like this "p1;5;4;7;6;5;0" so I will have to read that line in and split it and put each number into the array. then do the next for the next line. After i put it into the array i have to turn it into a polynomial p1: 5x^4+7x^6+5. and then i will have to add the two polynomials together or subtract them, depending on which the user chooses to do. – Dave Aug 30 '14 at 06:40

4 Answers4

1

Why not use a (Hash) Map where the key is the variable name?

Map polys = new HashMap();

int count=0;

For ...
    string key = String.format("p%02d",count++);
    polys.put(key, newPoly(...));

I'd have to look up the String.format but something like that.

The order needs to be preserved so just choose long enough zero padded keys that you can sort. And/or use a linkedHashMap which keeps the insertion order.

HankCa
  • 9,129
  • 8
  • 62
  • 83
  • It is mandatory to use ArrayList class. All of the polynomials must be stored in an ArrayList. – Dave Aug 30 '14 at 06:32
  • Must be stored in an ArrayList or the ArrayList must be returned as part of the API. If the latter then you can simply: – HankCa Aug 30 '14 at 07:26
  • Let me try again (silly 5 min timeout!): When you say "it must be stored in an ArrayList", do you mean that it must be STORED in one or the ArrayList must be returned as part of the API. If the latter then you can simply: public List getPolynomialArray() { return new ArrayList(map.values()); } If the former and the array index isn't sufficient as user2357112 suggested then you'd need to use reflection to create variables. – HankCa Aug 30 '14 at 07:33
0

First of all, you can't do this using variables. Java does not allow you to declare variables dynamically. It is not that kind of programming language ...


If it is mandatory that the polynomials are stored in an ArrayList then:

  • get the user to refer to the polynomials by number (i.e. position in the list) instead of by name, or

  • create a hashmap that maps from names to positions in the list, or

  • store the polynomials in BOTH an ArrayList<String> AND a HashMap<String, String>.

Actually, I think that you may have misinterpreted the requirements for your programming exercise. I suspect that you are asked to represent each individual polynomial as an ArrayList (or a custom class that has an ArrayList inside it). Representing a polynomial as a String doesn't allow you to do any operations on it ... without first parsing the String and turning it into another form.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I am asked to create a program for processing single-variable polynomials represented as ArrayLists . But apparently I do not know how and am confused. – Dave Aug 30 '14 at 06:48
  • You might want to rephrase that comment. Hint: what do you mean by "that"? – Stephen C Aug 30 '14 at 06:50
  • Well yes. I think you are confused. Your requirements do not say that you can't use a HashMap to map names to polynomials. – Stephen C Aug 30 '14 at 07:08
  • No I guess not but I am not too familiar with HashMap either. – Dave Aug 30 '14 at 07:15
0

As Stephen's answer and you said that arraylist is mandatory you could still use ArrayList

ArrayList numbers = new ArrayList();


HashMap<String,ArrayList> myPolys = new HashMap<String,ArrayList>();

and to use the HashMap

myPolys.put(what do you want to call them or your so "called" variables , numbers);
Ker p pag
  • 1,568
  • 12
  • 25
0

As you absolutely need to use ArrayList class to store your polynomials you can use its add(int index, E Element) method as follows:

List polynomials= new ArrayList(); 
for(int k=0;k < counter;k++){
    polynomials.add(k, new Poly(...));
}

You won't have P0, P1, ... but polynomials.get(0), polynomials.get(1), ... Thanks to gmhk in this.

Community
  • 1
  • 1
Kaem Jii
  • 82
  • 8
  • confused me with this what is it? – Dave Aug 30 '14 at 07:21
  • Here you add your polynomials in an ArrayList as you wanted. But you will access them through their indices in the ArrayList. It is why you see polynomials.get(1) to access the polynomial at index 1 (your P1). According to me best way is to use a Map. There you would store your polynomials with their String keys "P0", "P1", ... and access them with the Map [get method](http://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object-). But you want an ArrayList. I gave what I had. – Kaem Jii Aug 30 '14 at 08:44