如何使用 charAt() 和 br.skip() 克服 \n(Enter)?
How to overcome \n(Enter) using charAt() and br.skip()?
在这个给定的程序输出中显示空白,原因是 name.The 原因是我们使用了 read()
和 readLine()
方法。
//Employee details
import java.io.*;
public class Empdata {
public static void main(String[] args) throws IOException {
// create BufferedReader object to accept data from keyboard
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
// accept employee details
System.out.print("Enter id:");
int id = Integer.parseInt(br.readLine());
System.out.print("Enter sex(M/F):");
char sex = (char) br.read();
System.out.print("Enter name:");
String name = br.readLine();
// display the employee details
System.out.println("Id:" + id);
System.out.println("Sex:" + sex);
System.out.println("Name:" + name);
}
}
输出:
输入id:10
输入性别(M/F):M
输入姓名:id:10
性别:M
姓名:
有两种方法可以解决这个问题:
br.readLine().charAt(0)
br.skip(2)
public class Empdata {
public static void main(String[] args) throws IOException {
// create BufferedReader object to accept data from keyboard
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
// accept employee details
System.out.print("Enter id:");
int id = Integer.parseInt(br.readLine());
System.out.print("Enter sex(M/F):");
char sex = (char) br.read();
//Solution #1 : char sex = br.readLine().charAt(0)
//Solution #2 : skip 2 characters if using (char) br.read()
br.skip(2)
System.out.print("Enter name:");
String name = br.readLine();
// display the employee details
System.out.println("Id:" + id);
System.out.println("Sex:" + sex);
System.out.println("Name:" + name);
}
}
输出:
Enter id:10
Enter sex(M/F):M
Enter name:Vinay
id:10
sex: M
Name:Vinay
在这个给定的程序输出中显示空白,原因是 name.The 原因是我们使用了 read()
和 readLine()
方法。
//Employee details
import java.io.*;
public class Empdata {
public static void main(String[] args) throws IOException {
// create BufferedReader object to accept data from keyboard
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
// accept employee details
System.out.print("Enter id:");
int id = Integer.parseInt(br.readLine());
System.out.print("Enter sex(M/F):");
char sex = (char) br.read();
System.out.print("Enter name:");
String name = br.readLine();
// display the employee details
System.out.println("Id:" + id);
System.out.println("Sex:" + sex);
System.out.println("Name:" + name);
}
}
输出:
输入id:10
输入性别(M/F):M
输入姓名:id:10
性别:M
姓名:
有两种方法可以解决这个问题:
br.readLine().charAt(0)
br.skip(2)
public class Empdata {
public static void main(String[] args) throws IOException {
// create BufferedReader object to accept data from keyboard
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
// accept employee details
System.out.print("Enter id:");
int id = Integer.parseInt(br.readLine());
System.out.print("Enter sex(M/F):");
char sex = (char) br.read();
//Solution #1 : char sex = br.readLine().charAt(0)
//Solution #2 : skip 2 characters if using (char) br.read()
br.skip(2)
System.out.print("Enter name:");
String name = br.readLine();
// display the employee details
System.out.println("Id:" + id);
System.out.println("Sex:" + sex);
System.out.println("Name:" + name);
}
}
输出:
Enter id:10
Enter sex(M/F):M
Enter name:Vinay
id:10
sex: M
Name:Vinay