java中字符串输入
字符串是在Java中表示文本数据的基本数据类型。有几种方法可以在Java中输入字符串:
- 使用字符串文字:最简单的方法是使用字符串文字,即用双引号(" ")或单引号(')括起来的文本。例如:
String myString = "Hello world!";登录后复制 - 使用Scanner类:Scanner类可以从各种输入源(如键盘、文件等)读取输入。要使用Scanner输入字符串,请使用nextLine()方法。例如:
import java.util.Scanner;
public class StringInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String myString = scanner.nextLine();
scanner.close();
}
}登录后复制
- 使用BufferedReader类:BufferedReader类可以从字符流中读取文本。要使用BufferedReader输入字符串,请使用readLine()方法。例如:
立即学习“Java免费学习笔记(深入)”;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StringInput {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string: ");
String myString = reader.readLine();
reader.close();
}
}登录后复制
- 使用控制台System.in:可以使用控制台System.in从标准输入读取字符串。要使用System.in输入字符串,请使用read()或readLine()方法。例如:
import java.io.IOException;
public class StringInput {
public static void main(String[] args) throws IOException {
System.out.print("Enter a string: ");
byte[] bytes = new byte[100];
System.in.read(bytes);
String myString = new String(bytes).trim();
}
}登录后复制以上就是java字符串怎么输入的详细内容,更多请关注php中文网其它相关文章!


MP2 天前
发表在:MagicEXIF通用注册机 v1.13明亮的 旅行分享! 做得真好。
BrendanWaida6 天前
发表在:11日20日,星期四,在这里每天60秒读懂世界!При выборе автономно...
JosephJaf8 天前
发表在:MagicEXIF通用注册机 v1.13我尊重这样的项目, 这里展示真正的旅游。...
Frankcic9 天前
发表在:11日20日,星期四,在这里每天60秒读懂世界!Для блога может быть...
Stevedaf18 天前
发表在:MagicEXIF通用注册机 v1.13所有文章都令人印象深刻。继续保持 真诚。...
Stevedaf18 天前
发表在:Intel XTU中文补丁 1.13我经常访问 关于旅行的资源。有趣阅读游记...
Stevedaf18 天前
发表在:MagicEXIF通用注册机 v1.13我常常想, 能像你们一样多旅行。感谢激励...
Stevedaf18 天前
发表在:Intel XTU中文补丁 1.13很高兴阅读 有用的内容。十分 很有意思。...
Stevedaf19 天前
发表在:MagicEXIF通用注册机 v1.13我早就想, 能像你们一样多旅行。谢谢启发...
Stevedaf19 天前
发表在:Intel XTU中文补丁 1.13我一直梦想, 那么放松地度假。感谢激励。...