2012-11-16 61 views
6

Tôi có một chút phiền toái ở đây; trong đó tôi không thể lấy đầu vào đúng cách. Tôi đã luôn nhập thông tin qua Scanner và không được sử dụng để BufferedReader.Lấy đầu vào với BufferedReader trong Java


ĐẦU VÀO FORMAT


First line contains T, which is an integer representing the number of test cases. 
T cases follow. Each case consists of two lines. 

First line has the string S. 
The second line contains two integers M, P separated by a space. 

VÍ DỤ

Input: 
2 
AbcDef 
1 2 
abcabc 
1 1 

Mã của tôi cho đến nay:


public static void main (String[] args) throws java.lang.Exception 
{ 
    BufferedReader inp = new BufferedReader (new InputStreamReader(System.in)); 
    int T= Integer.parseInt(inp.readLine()); 

    for(int i=0;i<T;i++) { 
     String s= inp.readLine(); 
     int[] m= new int[2]; 
     m[0]=inp.read(); 
     m[1]=inp.read(); 

     // Checking whether I am taking the inputs correctly 
     System.out.println(s); 
     System.out.println(m[0]); 
     System.out.println(m[1]); 
    } 
} 

Khi nhập vào ví dụ trên cho thấy, tôi nhận được kết quả như sau:

AbcDef 
9 
49 
2 
9 
97 
+1

m [0] = inp.read(); đang đọc một byte hoặc một cái gì đó. làm một readline vào một chuỗi và chia nó để có được hai lĩnh vực, sau đó phân tích chúng thành ints. –

+0

'inp.read()' sẽ đọc một ký tự đơn (16 bit) không phải byte (8 bit). –

Trả lời

13

BufferedReader#read đọc ký tự đơn [0 đến 65535 (0x00-0xffff)] từ luồng, vì vậy không thể đọc số nguyên duy nhất từ ​​luồng.

  String s= inp.readLine(); 
      int[] m= new int[2]; 
      String[] s1 = inp.readLine().split(" "); 
      m[0]=Integer.parseInt(s1[0]); 
      m[1]=Integer.parseInt(s1[1]); 

      // Checking whether I am taking the inputs correctly 
      System.out.println(s); 
      System.out.println(m[0]); 
      System.out.println(m[1]); 

Bạn cũng có thể kiểm tra Scanner vs. BufferedReader.

2

Id sự cố do inp.read();method. Nhân vật trả về của nó tại một thời điểm và bởi vì bạn đang lưu trữ nó vào loại int của mảng để chỉ lưu trữ giá trị ascii của điều đó.

gì bạn có thể làm đơn giản

for(int i=0;i<T;i++) { 
    String s= inp.readLine(); 
    String[] intValues = inp.readLine().split(" "); 
    int[] m= new int[2]; 
    m[0]=Integer.parseInt(intValues[0]); 
    m[1]=Integer.parseInt(intValues[1]); 

    // Checking whether I am taking the inputs correctly 
    System.out.println(s); 
    System.out.println(m[0]); 
    System.out.println(m[1]); 
} 
0

Bạn không thể đọc được các số nguyên cá nhân trong một dòng riêng biệt sử dụng BufferedReader là bạn sử dụng Scanner lớp. Mặc dù, bạn có thể làm một cái gì đó như thế này liên quan đến truy vấn của bạn:

import java.io.*; 
class Test 
{ 
    public static void main(String args[])throws IOException 
    { 
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
     int t=Integer.parseInt(br.readLine()); 
     for(int i=0;i<t;i++) 
     { 
     String str=br.readLine(); 
     String num[]=br.readLine().split(" "); 
     int num1=Integer.parseInt(num[0]); 
     int num2=Integer.parseInt(num[1]); 
     //rest of your code 
     } 
    } 
} 

Tôi hy vọng điều này sẽ giúp bạn.

Các vấn đề liên quan