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('
');      }   }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job