mercredi 9 janvier 2019

Why is hasNext() skipping my last input, It return ans for my first inputs and then goes to check my last input?

I have to check whether all my input are ip addresses but my program return ans for the first 5 inputs and then goes to test the 6 th input instead of testing them all together.

import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class regex{
    public static void main(String[] args){
        //System.out.println("Enter an ip address");
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String ip = in.next();
            System.out.println(ip.matches(new Myregex().pattern));
        }
        in.close();
    }
}
class Myregex{
    String num ="([01]?\\d{1,2}|2[0-4]\\d|25[0-5])";
    String pattern = num+"."+num+"."+num+"."+num;

}

INPUT:

000.12.12.034

121.234.12.12

23.45.12.56

00.12.123.123123.123

122.23

true

true

true

false

false

Hello.IP

false

It should come like this:

000.12.12.034

121.234.12.12

23.45.12.56

00.12.123.123123.123

122.23

Hello.IP

true

true

true

false

false

false

Aucun commentaire:

Enregistrer un commentaire