Data input

Data input

I need to pass some data from the user to my applet (or application) which is to be entered from the keyboard, using TextField.

The data is repetitive (it fills an array), so when the user enters data for one field of an array, he is to enter another, and another and so on, until all the data is entered.After that, user must enter data from the same TextField into another array, for example, and so on, until everything is known.Because of the amount of data, I can’t use parameters in the APPLET tag.

This whole thing should be similar to something like this in C:

while(entering_data1){   printf(“Please enter data”);   scanf(“%s”,&data1[x]);}…while(entering_data2){   printf(“Please enter data”);   scanf(“%s”,&data2[y]);}

The following code fills a two-dimensional array with numbers read fromstdin. The code takes advantage of the fact that Java methods canreturn arrays as values, hence the line:

 nums[i] = readArray(, M);
can be placed in a loop, and the dirty work of actually readingnumbers can be encapsulated inside readArray.The only option remaining was to read an entire line with
This works, but maybe not for long. JDK1.1 declares readLine obsolete. The preferred technique is to use the readLine method of the BufferedReader class:
BufferedReader din = new BufferedReader(new InputStreamReader(in));   …   result[i] = din.ReadLine();
Hope this gives you some ideas:
import*;public class ReadDemo {   private static int[] readArray(InputStream in, int m) {      int[] result = new int[m];      DataInputStream din = new DataInputStream(in);      String digits;      for(int i = 0; i < m; i++) {         System.out.print("enter a number: ");         // System.out.flush();         try {             digits = din.readLine();             result[i] = Integer.valueOf(digits).intValue();          }         catch(IOException e) {            System.out.println('
' + e.toString());            System.exit(1);         }      }            return result;   }   public static void main(String[] args) {      int M = 2, N = 2;      int[][] nums = new int[N][M];      for(int i = 0; i < N; i++)          nums[i] = readArray(, M);      System.out.println("Here are your numbers: ");      for(int j = 0; j < N; j++) {         for(int k = 0; k < M; k++)            System.out.print(" " + String.valueOf(nums[j][k]));         System.out.println('
');      }   }}

See also  11 Effective Keyword Research Tools and Techniques

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist