ControlP5 Logic problem
in
Contributed Library Questions
•
1 year ago
I have a java file and a processing file.
FoodData.txt looks like this:
ham
23
30
50
50
100
corn
5
10
5
10
15.5
In the function checkForFile(),
println (food[x].toString()) gives the right information as:
ham
23
30
50
50
100
corn
5
10
5
10
15.5
However, when I use the function LIST() to the textarea called searchList.
The information of ham changes into that of the corn like this:
ham
5
10
5
10
15.5
corn
5
10
5
10
15.5...
you need to compare the two in the interactive console, not on the actual window.
- import controlP5.*;
- ControlP5 controlP5;
- //INPUT
- Textfield search;
- //OUTPUT
- Textarea searchList;
- Textarea console;
- //FOOD
- final int maxFood = 100;
- private Food []food = new Food[maxFood];
- private int numFood = 0;
- void setup()
- {
- size(1000,500);
- background(255);
- frameRate(30);
- controlP5 = new ControlP5(this);
- //Search
- search = controlP5.addTextfield("",50,50,150,20);
- controlP5.addButton("SEARCH",0,210,50,50,20);
- searchList = controlP5.addTextarea("lable1","List",50,90,150,300);
- searchList.setColorBackground(0xff0066cc);
- //List
- controlP5.addButton("LIST",1,210,70,50,20);
- //Console
- console = controlP5.addTextarea("Console","Console",50,400,300,100);
- console.setColorBackground(0xff0066cc);
- }
- void draw()
- {
- }
- //Search Button
- public void SEARCH(int theValue) {
- console.setText("Searching...");
- if(checkForFile()) {
- console.setText("Data file located and loaded with "+ numFood + ".");
- }
- }
- //List
- public void LIST(){
- String total ="";
- for (int x = 0; x<numFood;x++)
- {
- total += food[x].toString() +"\n";
- println (food[x].toString());
- }
- searchList.setText(total);
- }
- //Check for the validity of database
- private boolean checkForFile() {
- try {
- BufferedReader inFile = createReader("FoodData.txt");
- String name;
- int x = 0;
- double nutrient[] = new double[5];
- while((name=inFile.readLine()) != null) {
- for (int p = 0; p<5; p++)
- {
- nutrient[p] = Double.parseDouble(inFile.readLine());
- }
- food[x] = new Food(name,nutrient);
- println(food[x].toString());
- x++;
- }
- numFood = x;
- LIST(); //List function
- inFile.close();
- return true;
- }
- catch (IOException io) {
- println(io.getMessage());
- }
- return false;
- }
- public class Food {
- private double info[];
- private String name;
- public Food(String name,double []info) {
- setFoodName(name);
- setInfo(info);
- }
- public String getFoodName() {
- return name;
- }
- public void setFoodName(String name) {
- this.name = name;
- }
- public double[] getInfo() {
- return info;
- }
- public void setInfo(double[] info) {
- this.info = info;
- }
- public String toString() {
- String result = "";
- result += name +"\n-----------\n"+"Calorie: "+ info[0]+"\n"+
- "Fat: "+info[1]+ "\n" + "Cholesterol: "+ info[2] +"\n"+"Sodium: " + info[3] +"\n" +
- "Protein: "+ info[4]+"\n";
- return result;
- }
- }
1