string - How do I have a program create its own variables with Java? -


i start off saying if common knowledge, please forgive me , have patience. new java. trying write program store many values of variables in sort of buffer. wondering if there way have program "create" own variables, , assign them values. here example of trying avoid:

package test;  import java.util.scanner;  public class main {      public static void main(string args[]) {          int inputcachenumber = 0;         //text file:         string userinputcache1 = null;         string userinputcache2 = null;         string userinputcache3 = null;         string userinputcache4 = null;          //program         while (true) {             scanner scan = new scanner(system.in);             system.out.println("user input: ");             string userinput;             userinput = scan.nextline();              // in text file             if (inputcachenumber == 0) {                 userinputcache1 = userinput;                 inputcachenumber++;                 system.out.println(userinputcache1);             } else if (inputcachenumber == 1) {                 userinputcache2 = userinput;                 inputcachenumber++;             } else if (inputcachenumber == 2) {                 userinputcache3 = userinput;                 inputcachenumber++;             } else if (inputcachenumber == 3) {                 userinputcache4 = userinput;                 inputcachenumber++;             }             // , on          }      } } 

so try summarize, know if there way program set unlimited number of user input values string values. wondering if there way can avoid predefining variables may need. reading, , patience , help! ~rane

you can use array list data structure.

the arraylist class extends abstractlist , implements list interface. arraylist supports dynamic arrays can grow needed.

for example:

list<string> userinputcache = new arraylist<>(); 

and when want add each input array like

if (inputcachenumber == 0) {     userinputcache.add(userinput); // <----- here     inputcachenumber++; } 

if want traverse array list can follows:

for (int = 0; < userinputcache.size(); i++) {     system.out.println(" user input " + userinputcache.get(i)); }  

or can use enhanced loop

for(string st : userinputcache) {     system.out.println("your user input " + st); } 

note: better put scanner in try catch block resource not worried if close or not @ end.

for example:

try(scanner input = new scanner(system.in)) {     /*     **whatever code have put here**     point madprogrammer:     beware of it, that's all. lot of people have multiple stages in        programs may require them create new scanner after try-block     */  } catch(exception e) {  } 

for more info on arraylist

http://www.tutorialspoint.com/java/java_arraylist_class.htm


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -