2013-10-09 41 views
0

如果你有一個文件,第一行是數據集的數量,其餘的,你必須乘以和輸出兩個整數,你會怎麼做?如何在文件ijn java中分割數字?

The file is 
    9 
    10 2 
    11 1 
    8 2 
    7 9 
    -1 10 
    3 9 
    6 3 
    18 2 
    7 3 
And the output is supposed to be 
    20 
    11 
    16 
    63 
    -10 
    27 
    18 
    36 
    21 

(注意這是在Java中,並且如果在輸入第一數字被改變時,與該輸出的變化,並且數字數應該是與其他數字互換)

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
public class Basic_Attempt { 

    public static void main(String[] args) { 

     // Location of file to read 
     File file = new File("data.txt"); 

     try { 

      Scanner scanner = new Scanner(file); 

      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       System.out.println(line); 
      } 
      scanner.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我可能會使用太多的代碼來模擬文件,但我需要知道如何將掃描器輸入實際拆分爲數組,以及如何檢查每行中的內容。有沒有辦法在java中做到這一點?

+0

問題要求代碼必須表現出對問題的理解最小正在解決。包括嘗試解決方案,爲什麼他們沒有工作,以及預期的結果。 – initramfs

+0

我開始閱讀[Basic I/O](http://docs.oracle.com/javase/tutorial/essential/io/),用['String'](http:// docs.oracle.com/javase/7/docs/api/java/lang/String.html)和['Integer'](http://docs.oracle.com/javase/7/docs/api/java/lang /Integer.html)類,並敲出一些東西... – MadProgrammer

+0

如果我不得不這樣做,我不會這樣做。 –

回答

1

檢查下面的代碼:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

class Test{ 

    public static void main(String args[]){ 
     Integer[] test = getData("file1.jt"); 
     for(Integer t: test){ 
      System.out.println(t); 
     } 
    } 

    public static Integer[] getData(String pathtofile){ 
     BufferedReader br = null; 
     Integer[] ret = null; 
     try { 
      String sCurrentLine; 
      Integer totNumber; 
      br = new BufferedReader(new FileReader(pathtofile)); 
      totNumber = Integer.parseInt(br.readLine()); 

      if(totNumber > 0){ 
       ret = new Integer[totNumber]; 
       for(int i=0;i<totNumber;i++){ 
        if(((sCurrentLine = br.readLine()) != null)){ 
         String[] pieces = sCurrentLine.split(" "); 
         if(pieces.length==2){ 
          ret[i] = Integer.parseInt(pieces[0]) * Integer.parseInt(pieces[1]); 
         } 
        } 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (NumberFormatException e){ 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null) br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
     return ret; 
    } 
} 
1
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class hello { 

    public static void main(String[] args) { 

     // Location of file to read 
     File file = new File("C:\\Users\\k\\Desktop\\新建文本文檔.txt"); 

     try { 

      Scanner scanner = new Scanner(file); 

      int lineNum = scanner.nextInt(); 
      scanner.nextLine(); 
      while (lineNum > 0) { 
       String[] ss = new String[2]; 
       String line = scanner.nextLine(); 
       lineNum --; 
       ss = line.split("\t"); 

       int first = Integer.parseInt(ss[0]); 
       int second = Integer.parseInt(ss[1]); 
       int result = first * second; 
       System.out.println(String.valueOf(result)); 
      } 
      scanner.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
}