我想将用户输入转换为字符串数组,然后编辑该数组以获取关键字以更改它们,但是我不知道如何执行此操作并立即遇到尝试的问题解决如何在没有数组超出范围的情况下对此进行编码。
我唯一的代码是:
import java.util.*;
class FilterTest{
public static void main (String[] args){
Scanner scan = new Scanner (System.in);
//String[] inputMessage = new String (); I don't know how to do the length here.
input.nextString();
inputMessage=input.nextString;
}
}
答案 0 :(得分:1)
以下是ArrayList
的示例。
public static void main(String[] args) {
ArrayList<String> inputMessage = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
while (true) {
inputMessage.add(sc.next());
}
}
答案 1 :(得分:0)
import java.util.*
class FilterTest{
public static void main (String[] args){
Scanner scan = new Scanner (System.in);
Vector<String> all=new Vector<String>();
while(true){
String in=scan.nextString();
if(in.length()==0){
break;
}
all.add(in);
}
String array[]=all.toArray(new String[all.size()]);
// here you go
}
}
答案 2 :(得分:0)
下面的代码将所有输入存储到string类型的ArrayList中,直到输入为“quit”。
import java.util.*;
class FilterTest{
public static void main (String[] args){
Scanner scan = new Scanner (System.in);
ArrayList<String> full= new ArrayList<String>();
// We use a do-while loop to be able to check the condition of the loop AFTER each round
// since we want to check if the user typed 'quit' AFTER he typed it.
do {
// Read input
String input = input.nextString();
// Add it to the ArrayList
full.add(input);
}
while(!input.equals("quit")); // Cancel as soon as the user types "quit"
for(String inputString: full) {
// Do whatever you want to do with each input
}
}
}