java - Having problems with arrays (involving user multiple inputs)? -


int number = keyboard.nextint(); string [][] myarray = new string[number][1] (int = 0; < x; i++) {        system.out.println("enter food name: ");     myarray[i][0] = keyboard.nextline();      (int j = 0; j < 1; j++)     {        system.out.println("enter type of food: ");     myarray[i][j] = keyboard.nextline();     } } 

here code ask. want print out these outputs when run program:

enter food name: (where user type or input) enter type of food: (where user type or input)  (my problem prints out instead first loop:) enter food name: enter type of food: (where user type or input) 

with no way of entering food item. after first loop, program normal output want, how change code first loop take in user input "enter food name: "?

thanks in advance.

edit: sorry not seeing question beforehand 1 not loop if enter empty string before next input, loop not work. there fix this?

edit: got work. everyone.

you might try code below (this allows information entered until user types end .. rather requiring user know how many items enter. also, method created handle input .. can improve, adjust method easily.

import java.io.objectinputstream.getfield; import java.util.arraylist; import java.util.scanner;  public class foodarray {  private static string[][] foodinformation;  /**  * @param args  */ public static void main(string[] args) {     // todo auto-generated method stub      scanner keyboard = new scanner(system.in);     foodinformation = getfoodinformation(keyboard);  }  private static string[][] getfoodinformation(scanner inputdevice) {     string[][] result = null;     boolean isstoping = false;     arraylist<string> holdname = new arraylist<string>();     arraylist<string> holdtype = new arraylist<string>();      while (!isstoping) {         system.out.println("enter food name (or end exit): ");         string foodname = inputdevice.nextline().trim();         isstoping = "end".equalsignorecase(foodname);         if (!isstoping) {             system.out.println("enter food type");             string foodtype = inputdevice.nextline().trim();             holdname.add(foodname);             holdtype.add(foodtype);         }     }     int foodcount = holdname.size();     if (foodcount>0) {         result = new string[foodcount][foodcount];         (int i=0; < foodcount; i++) {             result[i][0] = holdname.get(i);             result[i][1] = holdtype.get(i);         }     }      return result;  }   } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -