简体   繁体   中英

why can't I compile with the Integer/System.readInt() function

public void main(String[] args) {
        int[] fylki = System.readInt(args[0]);
        while(!StdIn.isEmpty()){
            StdOut.print("Number of pairs in array: " + checkingPairs(fylki));
        }
    }

I am wondering what I'm doing incorrect here. I have imported the java.lang.*; and both the StdIn; and StdOut;. I first tried to have

public void main(String[] args) {
        int[] fylki = Integer.readInt(args[0]);
        while(!StdIn.isEmpty()){
            StdOut.print("Number of pairs in array: " + checkingPairs(fylki));
        }
    }

but I got the same error for both

pairs.java:16: error: cannot find symbol
        int[] fylki = System.readInt(args[0]);
                            ^
  symbol:   method readInt(String)
  location: class System

What you did and where the problems are

The class System (or Integer ) doesn't have a method readInt() .

You can use Integer.parseInt() if you want an int instead.

Also, there is no class Stdin (as long as you haven't created it by yourself), but you could use System.in .

But there isn't a method isEmpty() in System.out (or InputStream , whatever you want). However, you could read one byte and check if the return value is 0(don't forget that this byte will be missing later).

By the way, java.lang.* is imported by default. You don't have to import it manually.

I think you mixed something up with other programming languages.

Here are a few things you could mean(as you pointed out in the comments, you want to get the ints from stdin ):

get int[] from first argument (with delimitor)

If you want to split it using some String, you can use it in combination with Streams( seq is the delimitor):

int[] fylki=Stream.of(args[0].split(seq)).mapToInt(Integer::parseInt).toArray(int[]::new);

This splits the first argument(in java args[0] is not the program path) by the delimitor (regex) and processes it using a Stream. It maps every part of the stream to an integer using Integer.parseInt() . Finally, it converts it to a new array.

get int[] from all arguments (1 arg =1 int)

If you want to parse all args to an int array(ints seperated by spaces), you could do this even easier:

int[] fylki=Stream.of(args).mapToInt(Integer::parseInt).toArray(int[]::new);

This is the same but you do not split it because you already have it as array.

read ints from stdin (with Scanner )

If you want to read ints from stdin (or System.in in java), you could use a Scanner .

At first, you could create a List to store the ints and a Scanner where you read the ints from.

After that, you read all the ints and store them to the List .

Finally, you copy the content of the List to the int[] you want and close the Scanner .

List<Integer> ints=new ArrayList<>();
Scanner scan=new Scanner(System.in);
while(scan.hasNextInt()){
    ints.add(scan.nextInt();
}
int[] fylki=ints.toArray(new int[0]);
scan.close();

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