简体   繁体   中英

How to add java.util.Scanner class to read two-dimensional array of float entries input?

I have a question about using java.util.Scanner class to read two-dimensional array of float entries input, I've tried to find on internet, but still can't work. The code show me errors. Anyone can check for me which part I get wrong? Below is my code:

    import java.util.Scanner;

public class CloneTwoDarray {
  public static float[][] clone(float[][] a) throws Exception {
  float b[][] = new float[a.length][a[0].length];

  for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
     b[i][j] = a[i][j];
     }
     }
       return b;
         }
   public static void main(String args[]) {


       float[][] a = new float[][];


       Scanner sc = new Scanner(System.in);

           System.out.println("Type nine float numbers two-dimensional array of similar type and size with line breaks, end by \"-1\":");
       String append = "";
         while (sc.hasNextLine()) {
         String input = sc.nextLine();
             if ("-1".equals(input)) {
    break;
          }
              lines.add(input);
         }

            sc.close();



            System.out.println("\n\nThe result is:\n");
           try {
             float b[][] = clone(a);
           for (int i = 0; i < a.length; i++) {
             for (int j = 0; j < a[0].length; j++) {
          System.out.print(b[i][j] + " ");
              }
            System.out.println();
            }
           } catch (Exception e) {
           System.out.println("Error!!!");
     }   
               }
       } 

The output error show me like below:

        C:\Users\User\AppData\Local\NetBeans\Cache\8.0.2\executor-snippets\run.xml:48: 
           Cancelled by user.
           BUILD FAILED (total time: 3 seconds)

Actually I want the output show me like below:

run:
Type nine float numbers two-dimensional array of similar type and size with line breaks, end by"-1":
1.513
2.321
3.421 
4.213
5.432 
6.123 
7.214
8.213 
9.213 
-1


The result is:

1.513 2.321 3.421 
4.213 5.432 6.123 
7.214 8.213 9.213 
BUILD SUCCESSFUL (total time: 11 second) 

Hope someone can help me solve this problem. Thanks.

I am not entirely sure what your goal is. Also, you define a method CloneTwoDarray that is never used. Your line formatting needs to be adjusted to be more readable.

As for the code itself, lines.add(input) accesses an object, lines , that was never defined.

float[][] a= new float[5][5]; is an example of declaring a two dimensional array with 5 rows and 5 columns . float[][] a = new float[][] is not a correct declaration.

Your stack trace does not provide enough information.

Java's garbage collection will generally take care of closing Scanner for you, you do not need to utilize sc.close() in this instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM