Java扫描器类和具有混合类型输入的主类

时间:2019-01-25 04:31:54

标签: java java.util.scanner

嗨,我是Java初学者,对我的作业一无所知。 这是我的问题: 我正在创建带有混合类型输入的扫描器类和主类。 代码是

import java.util.Scanner;
import java.util.ArrayList;

public class Games {

   private static Scanner input;

public Games() {
    input = new Scanner(System.in);
}

public void textParser() {

    ArrayList<String> word = new ArrayList<>();      
    while(input.hasNextLine()) {
        word.add(input.next());         
    }
     System.out.println("number of words " + word.size());
     System.out.println(word);


     ArrayList<Integer> num = new ArrayList<>();         
        while(input.hasNextInt()) {
            num.add(input.nextInt());           
        }
         System.out.println("number of numbers " +num.size());
         System.out.println(num);


    ArrayList<Double> number = new ArrayList<>();        
        while(input.hasNextDouble()) {
            number.add(input.nextDouble());         
        }
        System.out.println("number of doubles " +number.size());
        System.out.println(number);
}
}

如何从扫描程序类中调用方法“ public void textParser()”,主类如下所示

import java.util.Scanner;

public class Driver {
  public static void main(String[] args){
  String str = "Jack started the game form 9 pm to 11 pm, he got score 73.2"; 
  Scanner sentence = new Scanner(str);
  Games game = new Games();
  game.textParser();
  }
}

如何得出如下要求的结果:

  

单词数11

     

[杰克,开始,比赛,形式,下午,到,下午,他,得到,得分]

     

数字2个

     

[9,11]

     

双打数1

     

[73.2]

2 个答案:

答案 0 :(得分:0)

您不需要Scanner类,因为您已经有一个输入字符串。只需使用space delimiter将它分成几部分,然后进行检查即可。

public class Driver {
    public static void main(String[] args){
        String str = "Jack started the game form 9 pm to 11 pm, he got score 73.2";
        new Games().textParser(str);
    }
}

public class Games {

    public /* static */ void textParser(String str) {
        String[] parts = str.split("\\s+");

        List<String> words = getWords(parts);
        System.out.println("number of words " + words.size());
        System.out.println(words);

        List<Integer> num = getIntegerNumbers(parts);
        System.out.println("number of numbers " + num.size());
        System.out.println(num);


        List<Double> number = getDoubleNumbers(parts);
        System.out.println("number of doubles " + number.size());
        System.out.println(number);
    }

    private static List<String> getWords(String... parts) {
        return Arrays.stream(parts)
                     .filter(str -> {
                         try {
                             Double.parseDouble(str);
                             return false;
                         } catch(Exception e) {
                             return true;
                         }
                     })
                     .collect(Collectors.toList());
    }

    private static List<Integer> getIntegerNumbers(String... parts) {
        return Arrays.stream(parts)
                     .map(str -> {
                         try {
                             return Integer.parseInt(str);
                         } catch(Exception e) {
                             return null;
                         }
                     })
                     .filter(Objects::nonNull)
                     .collect(Collectors.toList());
    }

    private static List<Double> getDoubleNumbers(String... parts) {
        return Arrays.stream(parts)
                     .map(str -> {
                         try {
                             return Double.parseDouble(str);
                         } catch(Exception e) {
                             return null;
                         }
                     })
                     .filter(Objects::nonNull)
                     .filter(val -> Double.compare(val, val.intValue()) != 0)
                     .collect(Collectors.toList());
    }
}

输出:

number of words 11
[Jack, started, the, game, form, pm, to, pm,, he, got, score]
number of numbers 2
[9, 11]
number of doubles 1
[73.2]

答案 1 :(得分:-1)

请尝试以下代码。

class Games
{

    public static Scanner input;

    public Games()
    {
        // input = new Scanner(System.in);
    }

    public void textParser()
    {

        ArrayList<String> word = new ArrayList<>();
        while (input.hasNextLine())
        {
            word.add(input.next());
        }
        System.out.println("number of words " + word.size());
        System.out.println(word);

        // Scanner s = new Scanner("Jack started the game form 9 pm to 11 pm, he got score 73.2");

        input = new Scanner("Jack started the game form 9 pm to 11 pm, he got score 73.2");
        ArrayList<Integer> num = new ArrayList<>();
        while (input.hasNextLine())
        {
            input.next();
            while (input.hasNextInt())
            {
                num.add(input.nextInt());
            }

        }
        System.out.println("number of numbers " + num.size());
        System.out.println(num);
        // s.close();

        ArrayList<Double> number = new ArrayList<>();

        input = new Scanner("Jack started the game form 9 pm to 11 pm, he got score 73.2");
        while (input.hasNextLine())
        {
            input.next();
            while (input.hasNextDouble())
            {
                number.add(input.nextDouble());
            }

        }
        input.close();
        System.out.println("number of doubles " + number.size());
        System.out.println(number);
    }
}

和驱动程序类-

public class Driver 
{

    public static void main(String[] args)
    {
        String str = "Jack started the game form 9 pm to 11 pm, he got score 73.2";
        Scanner sentence = new Scanner(str);
        Games game = new Games();
        Games.input = sentence;
        game.textParser();
    }
}