Removing characters before adding to array in Java -


first off aware of arraylists , not want use them program. working on program local zoo (very beginning stages , not final version). takes user input , allows them enter animals , foods eat. program works fine if enter 1 food @ time, struggling figuring out how handle user input add more 1 food. (prompts guide them enter food separated commas, users may use spaces in between , need account either error message or accepting , csv.) there way , later retrieve values adding commas in? sorry pretty new java, help!

code user input:

 //create array     static string[][] animalfood;      string[][] addarray(int x) {       animalfood = new string[x][2];      scanner in = new scanner(system.in);     //loop through array , add amount of items user chose     (int row = 0; row < animalfood.length; row++){         system.out.print("enter animal name: ");         animalfood[row][0] = in.nextline();         system.out.print("enter food animal eats: ");         animalfood[row][1] = in.nextline();         }          system.out.println("thank adding information zoo!");         system.out.println("you entered following information: ");          //loop through , print informationa added         for(int = 0; < animalfood.length; i++)         {            for(int j = 0; j < animalfood[i].length; j++)            {             system.out.print(animalfood[i][j]);             if(j < animalfood[i].length - 1) system.out.print(" - ");              }             system.out.println(); } 

code search function:

string[][] searcharray(string name) {     string matchresult = "there no " + name + " found in zoo!";     string itemtomatch = name.touppercase();    string arrayitem = "";    string food = "";    (int = 0; < animalfood.length; i++) {          arrayitem = animalfood[i][0];          arrayitem = arrayitem.touppercase();          if(arrayitem.equals(itemtomatch)){              matchresult = "the animal " + name + " found in zoo! eats " + animalfood[i][1];                  }          else {             //nothing found          }           }     system.out.println(matchresult);     if (food != null) {     system.out.println(food);     }     return animalfood; } 

you can use split() method of string break array of strings separated given delimiter.

for(int row = 0; row < animalfood.length; row++){     system.out.print("enter animal name: ");     animalfood[row][0] = in.nextline();     system.out.print("enter foods animal eats: ");     //all of foods separated commas     string[] foods = in.nextline().split(",");     for(int = 0; < foods.length; i++) {         animalfood[row][i + 1] = foods[i];     } } 

you have increase max index of second dimension of animalfood, though, account multiple foods (or, although more complicated, dynamically resize array copying array greater max index).

for search, have nest loop list of items.

for (int = 0; < animalfood.length; i++) {      arrayitem = animalfood[i][0];      arrayitem = arrayitem.touppercase();      if(arrayitem.equals(itemtomatch)){          matchresult = "the animal " + name + " found in zoo! eats ";         //iterates on foods eats         for(int j = 1; j < animalfood[i].length; j++) {             //if last food in list             if(j == animalfood[i].length - 1) {                 matchresult += "and " + animalfood[i][j] + ".";             }             else {                 matchresult += animalfood[i][j] + ", ";             }         }              }      else {         //nothing found      }       } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -