devxlogo

Data input

Data input

Question:
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]);}

Answer:
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(System.in, 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
din.readLine()
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 java.io.*;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 

devx-admin

Share the Post: