nextline和readline
时间: 2023-06-03 18:01:52 浏览: 138
这两个操作都可以用于在读取文本文件时读取下一行。不过,nextline是Scanner类中的一个方法,它用于读取下一行,而readline则是BufferedReader类中的一个方法,它也用于读取下一行。两者之间的差别是nextline()返回的是一个包含该行的字符串,而readline()返回的是一个字符串。
相关问题
After reading the integer, the readPosInt method must also use the scanner’s nextLine method to read the single newline character that comes from the user pressing the Enter key on the keyboard after typing the integer (if you do not read this newline character using the nextLine method inside the readPosInt method, then the newline character will remain in the input stream, and, the next time you use the readLine method described above, the readLine method will just immediately read only the newline character from the input stream and return an empty string as result, without waiting for the user to type anything!写一个java文件
Sure, here is an example Java file that implements the readPosInt method with the described behavior:
```
import java.util.Scanner;
public class ReadPosIntExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = readPosInt(scanner);
System.out.println("You entered: " + num);
scanner.close();
}
public static int readPosInt(Scanner scanner) {
int num = 0;
boolean validInput = false;
while (!validInput) {
System.out.print("Enter a positive integer: ");
if (scanner.hasNextInt()) {
num = scanner.nextInt();
scanner.nextLine(); // consume newline character
if (num > 0) {
validInput = true;
} else {
System.out.println("Error: Input must be positive.");
}
} else {
System.out.println("Error: Input must be an integer.");
scanner.nextLine(); // consume invalid input line
}
}
return num;
}
}
```
This program prompts the user to enter a positive integer, reads an integer value using the scanner's nextInt method, and then uses the scanner's nextLine method to consume the newline character that follows the integer input. If the input is not a positive integer, the program displays an error message and asks the user to input again. The program continues to prompt the user until a valid positive integer is entered. Finally, the program prints the entered integer to the console.
string sid = textBox1.Text; string id; string prv; if (sid.Trim().Length < 18) { MessageBox.Show("省份证位数不足", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { StreamReader sr = File.OpenText(System.Environment.CurrentDirectory + @"C:\Users\Administrator\Desktop"); string nextLine; while ((nextLine = sr.ReadLine()) != null) { string[] ids = nextLine.Split(','); id = ids[0]; prv = ids[1]; //匹配身份证 if (sid.Substring(0, 2).Equals(id)) { textBox2.Text = prv; return; } } textBox2.Text = "暂无信息!"; } catch (Exception err) { Console.WriteLine(err); }错在哪里
代码中有两个问题:
1. 读取文件路径不正确,应该是 `File.OpenText(System.Environment.CurrentDirectory + @"\data.txt")`。
2. 变量 `id` 和 `prv` 没有初始化,应该改为 `string id = "";` 和 `string prv = "";`。
修改后的代码如下:
```
string sid = textBox1.Text;
string id = "";
string prv = "";
if (sid.Trim().Length < 18)
{
MessageBox.Show("省份证位数不足", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
StreamReader sr = File.OpenText(System.Environment.CurrentDirectory + @"\data.txt");
string nextLine;
while ((nextLine = sr.ReadLine()) != null)
{
string[] ids = nextLine.Split(',');
id = ids[0];
prv = ids[1];
//匹配身份证
if (sid.Substring(0, 2).Equals(id))
{
textBox2.Text = prv;
return;
}
}
textBox2.Text = "暂无信息!";
}
catch (Exception err)
{
Console.WriteLine(err);
}
```
阅读全文