英文:
How to I go from a String to an Array
问题
import java.util.*;
public class scanner_LAB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a sentence with the word \"java\" in it: ");
// input and end program
System.out.println();
System.out.println("Done - Press enter key to end program");
String line = input.nextLine();
System.out.println(line);
// length
int length = line.length();
System.out.println("Your sentence length: " + length);
// upper case and lower case
String lowerCase = line.toLowerCase();
System.out.println(lowerCase);
String upperCase = line.toUpperCase();
System.out.println(upperCase);
System.out.println(lowerCase.indexOf("java"));
// java to Java
System.out.println(line.substring(0, line.toLowerCase().indexOf("java")) + "Java" + line.substring(line.toLowerCase().indexOf("java") + 4, line.length()));
// java to JAVA
System.out.println(line.substring(0, line.toLowerCase().indexOf("java")) + "JAVA" + line.substring(line.toLowerCase().indexOf("java") + 4, line.length()));
// string to arrays
String[] words = line.split(" ");
System.out.println(Arrays.toString(words));
}
}
英文:
I want to change my strings into array lists in my scanner program. I know that I have to use the .split method, but I am not sure how. I will use this example:
This is a good class with JAVA -> [This, is, a, good, class, with, Java]
This is what I have so far:
import java.util.*;
public class scanner_LAB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a sentance with the word \"java\" in it: ");
// input and end program
System.out.println();
System.out.println("Done - Press enter key to end program");
String line = input.nextLine();
System.out.println(line);
// length
int length = line.length();
System.out.println("Your sent " + length);
// upper case and lower case
String lowerCase = line.toLowerCase();
System.out.println(lowerCase);
String upperCase = line.toUpperCase();
System.out.println(upperCase);
System.out.println(lowerCase.indexOf("java"));
// java to Java
System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "Java" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// java to JAVA
System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "JAVA" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// string to arrays
}
}
</details>
# 答案1
**得分**: 1
你可以使用字符串的分割函数将字符串分割成数组。
String[] strArray = line.split(" ");
<details>
<summary>英文:</summary>
You can use the split function of string to split the string into array.
String[] strArray = line.split(" ");
</details>
# 答案2
**得分**: 0
```java
回答关于使用split方法的问题,以下是一般使用方法:
// 将字符串拆分为数组
String[] words = line.split(" ");
for (String word : words)
System.out.println(word);
我运行了你的代码,这对你的预期目的有效(我只是注释掉了与这个问题无关的一些代码)
import java.util.*;
public class scanner_LAB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入包含单词“java”的句子:");
// 输入并结束程序
//System.out.println();
//System.out.println("完成 - 按回车键结束程序");
String line = input.nextLine();
System.out.println(line);
// 长度
int length = line.length();
System.out.println("你的句子长度为 " + length);
// 转换为小写和大写
String lowerCase = line.toLowerCase();
System.out.println(lowerCase);
String upperCase = line.toUpperCase();
System.out.println(upperCase);
System.out.println(lowerCase.indexOf("java"));
// 将java替换为Java
//System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "Java" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// 将java替换为JAVA
//System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "JAVA" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// 字符串拆分为数组
String[] words = line.split(" ");
System.out.println(Arrays.asList(words));
}
}
英文:
To answer your question about using the split method, this is the general way to use it
// string to arrays
String[] words = line.split(" ");
for(String word: words)
System.out.println(word);
I ran your code and this works four your intended purpose (I've just commented out some of your code that wasn't related to this question)
import java.util.*;
public class scanner_LAB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a sentance with the word \"java\" in it: ");
// input and end program
//System.out.println();
//System.out.println("Done - Press enter key to end program");
String line = input.nextLine();
System.out.println(line);
// length
int length = line.length();
System.out.println("Your sent " + length);
// upper case and lower case
String lowerCase = line.toLowerCase();
System.out.println(lowerCase);
String upperCase = line.toUpperCase();
System.out.println(upperCase);
System.out.println(lowerCase.indexOf("java"));
// java to Java
//System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "Java" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// java to JAVA
//System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "JAVA" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// string to arrays
String[] words = line.split(" ");
System.out.println(Arrays.asList(words));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论