深入了解Java中String、Char和Int之间的相互转换
作者:Michaelhbjian 时间:2022-09-14 10:18:54
在做在线编程题目的时候,需要了解一下数据的输入格式。这样可以对数据处理有比较好的把握,不需要把太多的时间放在这个上面,注重主要的算法逻辑即可。这里总结一下,为之后笔试做个准备。
1.从终端输入的方式
Scanner类的使用方法:
Scanner scanner = new Scanner(System.in);
从终端获取输入流,输入流传入Scanner初始化对象时,作为参数传递进去。
Scanner类的重要几个方法:
next方法(读取一个字符)
1、一定要读取到有效字符后才可以结束输入(要是什么都不输入,则程序不结束)
2、对输入的有效字符之前遇到的空白,next方法会自动将其去掉。只有输入有效字符后才将其后面的输入的空白作为分隔符或者结束符。
next方法补鞥呢得到带有空格的字符串。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
String next = sc.next();
System.out.println(next);
}
}
nextLine方法(读取一行字符串)
1、以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空格。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()){
String next = sc.nextLine();
System.out.println(next);
}
}
nextInt
读取一个整数。有时候可以直接在终端获取一个整数,不需要在将String转换为int。这样可以减少程序的运行时间。
2.String与Char
String转Char[]:利用toCharArray()
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
char[] chars = line.toCharArray();
String转单个插入字符:利用charAt()
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
char charAt = line.charAt(2);
3.String与int
int转化为String:利用valueOf()
int n = 10;
String s = String.valueOf(10);
System.out.println(s);
String转int:利用Integer.parseInt(s);
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int i = Integer.parseInt(line);
System.out.println(i);
4.输入的格式
例如:
5,15 2,10
import java.util.Scanner;
/**
* @author zhoujian123@hotmail.com 2018/8/24 19:27
*/
public class ScannerTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String[] split = line.split(" ");
for (int i = 0; i < split.length; i++) {
String s = split[i];
int i1 = Integer.parseInt(s.split(",")[0]);
int i2 = Integer.parseInt(s.split(",")[1]);
System.out.println(i1+" "+i2);
}
}
}
开始做在线笔试题了!
来源:https://www.jianshu.com/p/12f1df3885bb
标签:java,string,char,int,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Springboot对配置文件中的敏感信息加密
2021-05-24 10:50:25
![](https://img.aspxhome.com/file/2023/0/64020_0s.jpg)
JVM代码缓存区CodeCache原理及用法解析
2023-08-09 06:02:29
![](https://img.aspxhome.com/file/2023/8/61768_0s.jpg)
Java用栈实现综合计算器
2021-08-16 02:01:36
一文详解Java中的Stream的汇总和分组操作
2021-12-24 17:32:54
![](https://img.aspxhome.com/file/2023/3/61313_0s.png)
详解Java包装类及自动装箱拆箱
2023-11-14 21:13:23
Dubbo实现分布式日志链路追踪
2023-08-23 21:00:54
![](https://img.aspxhome.com/file/2023/2/58292_0s.png)
Java里的static在Kotlin里如何实现
2023-08-24 20:25:33
Stream distinct根据list某个字段去重的解决方案
2022-06-22 22:23:26
![](https://img.aspxhome.com/file/2023/0/62380_0s.png)
Spring使用@Autowired为抽象父类注入依赖代码实例
2023-02-01 09:30:35
线程局部变量的实现 ThreadLocal使用及场景介绍
2023-11-10 03:19:26
![](https://img.aspxhome.com/file/2023/1/59031_0s.jpg)
Java设计模式之状态模式
2022-05-08 07:24:25
![](https://img.aspxhome.com/file/2023/3/64003_0s.png)
ShardingSphere数据分片算法及测试实战
2023-11-28 02:23:03
![](https://img.aspxhome.com/file/2023/8/59768_0s.png)
通过Docker启动Solace并在Spring Boot通过JMS整合Solace的操作方法
2023-07-11 09:52:28
![](https://img.aspxhome.com/file/2023/7/57507_0s.png)
聊一聊Java的JVM类加载机制
2023-11-08 03:58:37
![](https://img.aspxhome.com/file/2023/5/58755_0s.png)
Java动态代 理分析及简单实例
2023-11-24 21:14:56
Java实现多用户注册登录的幸运抽奖
2023-07-30 11:57:44
![](https://img.aspxhome.com/file/2023/5/57815_0s.png)
SpringBoot整合mybatis的方法详解
2023-09-02 06:23:57
![](https://img.aspxhome.com/file/2023/5/58455_0s.png)
Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法
2022-05-24 17:14:13
![](https://img.aspxhome.com/file/2023/3/63323_0s.png)
Java StringBuffer与StringBuilder有什么区别
2022-12-15 22:35:12
C# DataGridView添加新行的2个方法
2023-06-23 05:42:19