java中怎么获取控制台上一行以空隔_Java 从控制台输入中把一行中的以空格分隔的单词取出的函数(Token)...

我不是女神ヾ 2022-11-07 14:58 170阅读 0赞

//this is our first class which does not have a main

//method. Its purpose it to serve as a warehouse of

//related methods that can be accessed from any main

//method.

//This class contains methods useful for writing a parser.

// 1. public static String getKeyInput(){

// blocks program until user enters zero or

// more characters followed by a “enter”

// and returns input to calling program

// 2. public static String[] getTokens(String input){

// takes String and breaks into tokens defined

// by one or more white spaces and return array

// of String each element of which is an individual

// token

import java.io.*;

import java.util.*;

//must make this class public since it will be used outside of package

public class ParserUtils{

public static String getKeyInput(){

String input = null;

try{

BufferedReader in =

new BufferedReader(new InputStreamReader(System.in));

input = in.readLine();

}

catch (IOException ioe){

System.out.println(ioe);

}

return input;

}

public static String[] getTokens(String input){

int i = 0;

StringTokenizer st = new StringTokenizer(input);

int numTokens = st.countTokens();

String[] tokenList = new String[numTokens];

while (st.hasMoreTokens()){

tokenList[i] = st.nextToken();

i++;

}

return(tokenList);

}

}

其它java控制台输入:

import java.util.Scanner;

Scanner in = new Scanner(System.in); System.out.println(“Please input the search number:”); int num = in.nextInt();

发表评论

表情:
评论列表 (有 0 条评论,170人围观)

还没有评论,来说两句吧...

相关阅读