Java中next()和nextLine()区别及用法

今天在项目之余,到杭电上刷了一道题,那道题是1062题,程序本身不是难,但是在里面实现过程中,突然发现用的Scanner类进行输入的,用到了next和nextLine这两个方法,在输入过程中也遇到一些问题,接下来进行讲述自己遇到的问题,以及如何解决的

 

杭电1062题目:

 

Text Reverse

Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
 
 
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
 
 
Output
For each test case, you should output the text which is processed.
 
 
Sample Input
 
3 olleh !dlrow m'I morf .udh I ekil .mca
 
 
Sample Output
 
hello world! I'm from hdu. I like acm.
Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
 
 
大概的意思就是:
给出一个整数n,接下来输入n行字符串,然后每行字符串按照空格取每一个单词,然后将每个单词逆序输出
 
题目很简单,因此在这里直接贴代码,讲一下自己遇到的问题:
 
import java.util.Scanner;

public class Main {

	public static String reverse1(String s) {
		int length = s.length();
		if (length <= 1)
			return s;
		String left = s.substring(0, length / 2);
		String right = s.substring(length / 2, length);
		return reverse1(right) + reverse1(left);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNextInt()) {
			int num = in.nextInt();  // 这里进行整数输入n  标注为:1
			String str;
			in.nextLine();           // 这里开始没有加,出现了问题  标注为:2
			for (int i = 0; i < num; i++) {
				str = in.nextLine();       // 这里就是用来输入整行字符串  标注为:3
				String[] s = str.split(" ");
				for (int j = 0; j < s.length; j++) {
					if (j != s.length - 1)
					{
						String ss = reverse1(s[j]);
						System.out.print(ss + " ");
					}
					else
					{
						String ss = reverse1(s[j]);
						System.out.print(ss);
						int p = str.length() - 1;
						while (str.charAt(p) == ' ') {
							System.out.print(' ');
							p--;
						}
						System.out.println();
					}
				}
			}
		}
	}
}

在上面代码的注释处,分别标记了1,2,3点:
1、首先输入一个整数n,
2、最开始没有加上这一句,
3、最开始是str = in.next()
 
这里就出现了问题,
因为  next()  这个方法呢,你输入了一串字符串, 他只得到空格、tab键、回车的第一个字符串,比如:
hello world! 这个字符串,只得到 hello ,如果是循环的话,第二次就能得到 world!
这样的话就得不到后面要操作的事情,因此查资料后采用nextLine()这个方法,这就是你输入一行字符串,检测到回车之后就保存,比如:
hello world! 这个字符串,就得到了整个 hello world! 字符串
 
上面读取整行字符串用nextLine()即可解决,但是,又出问题了:
1、上面代码注释中,最开始没有第二个in.nextLine()这一条语句,出现了下面的第二点的问题:
2、问题:没有in.nextLine()的话,程序在debug模式下运行,发现直接先跳过第一次的str = in.nextLine();这条语句,以str = 空形式传递了值,因此,后面相当于做了一次空操作,输出了一个空行,问题在哪呢?看下面第三点:
3、查了资料,就是说,当next()、nextInt()、nextDouble()等等这些之后,你如果不再加一条in.nextLine()的话,下面如果用到了类似str = in.nextLine(); 这条语句的话,会首先读取上面next()、nextInt()、nextDouble()等等这些语句的回车作为一条默认的(为什么是这样的机制呢?还需要继续探索),因此,解决的办法看下面第四点:
4、就是在输入 next()、nextInt()、nextDouble()等等这些 之后,再用一个in.nextLine()这个来截取上面的一个回车操作,后面的nextLine()在第一次才能起作用
 
其实解释的自己不是很满意,因此希望大家一起来整理一下,百度了很多,各种copy,然后基本上千篇一律的答案,因此想总结一下,希望和大家一起总结一篇比较满意的出来~~

 

更多精彩敬请关注公众号

Java极客思维

微信扫一扫,关注公众号

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值