2014-02-26 23 views
1

我試圖讓掃描儀從外部數據文件,字母和數字讀取並將它們插入到單獨的數組中。我需要將前兩個字母分隔成不同的數組,並將這兩個數字中的每一個分隔成不同的數組,並且在做這件事時遇到了一些麻煩。Java將混合數據讀入不同的陣列

我試過做Scanner.nextScanner.nextLine和解析它,但它似乎不工作,任何人都可以給我一個正確的方向推動?

數據文件看起來像這樣

FH45 36.266 

MF18 46.967 

MH29 33.309 

MT95 36.983 

MR30 23.1768 

FF31 42.55 

FF65 39.96 

FT50 30.962 

FR80 19.375 

MH36 24.017 

QF40 30 

MQ40 30 

這裏是我的代碼,到目前爲止,我所擁有的是數組,我想這四個不同的東西分成四個不同的陣列,我不想打印出任何東西。

import java.io.*; 
import java.util.*; 
public class Height 
{ 
    public static void main(String[]args) throws IOException 
    { 
     Scanner inFile = new Scanner(new FileInputStream("info.dat")); 
     char Bone[] = new char[12]; 
     char Gender[] = new char[12]; 
     int Age[] = new int[12]; 
     double Length[] = new double[12]; 

    } 
} 
+0

請編輯您的帖子,爲我們提供迄今爲止的代碼,並且比「看起來沒有用」更具體 - 您是否收到錯誤,意外的輸出等? – mbroshi

+0

我所擁有的是掃描儀和我想要數據進入的數組,我不知道我該怎麼做才能分割數據並將其存入數組。大多數時候它不會編譯正確,或者它不會將正確的信息放入數組中。 –

+0

工作與否,最好包括你有的任何代碼。通常情況下,人們不會爲你編碼,並且通常會對你投票,但看看下面的5個答案,看起來就像你的幸運日。 – mbroshi

回答

0
while(scan.hasNext()) 
      { 
       System.out.print("Letter is" +scan.next()); 
       System.out.println("Number is"+Double.parseDouble(scan.next()));  
      } 

如果使用nextLine()方法next(後),那麼它將返回編號以字母和number.So之間的空間一起使用trim()方法去除地方nextLine()空間或next()

0
import java.util.*; 
import java.io.*; 

public class HelloWorld{ 
public static void main(String []args) throws IOException{ 
    Scanner s = null; 
    String str = "FH45 36.266\nMF18 46.967"; 
    ArrayList<String> array1 = new ArrayList<String>(); 
    ArrayList<Double> array2 = new ArrayList<Double>(); 
    try { 
     //uncomment this line for file 
     //s = new Scanner(new BufferedReader(new FileReader("xanadu.txt"))); 
     s = new Scanner(new BufferedReader(new StringReader(str))); 

     while (s.hasNext()) { 
      String token = s.next(); 
      try{ 
       double d = Double.parseDouble(token); 
       array2.add(d); 
      } 
      catch(NumberFormatException nfe){ 
       array1.add(token); 
      } 
     } 
    } finally { 
     if (s != null) { 
      s.close(); 
     } 

     System.out.println("Printing Letter Array "); 
     for(String out: array1){ 
      System.out.print(out+" "); 
     } 

     System.out.println("\nPrinting Numeric Array"); 
     for(Double out: array2){ 
      System.out.print(out+" "); 
     } 
    } 
} 

} 
0

我假設你只想打印包含字符和整數的部分。在這種情況下,我想你可以用下面的辦法: -

import java.util.*; 
public class Demo 
{ 
public static void main(String[] args) 
{ 
    ArrayList<Integer> Int = new ArrayList<Integer>(); 
    ArrayList<Double> duble = new ArrayList<Double>(); 
    ArrayList<Character> FirstChar = new ArrayList<Character>(); 
    ArrayList<Character> SecondChar = new ArrayList<Character>(); 

    try 
    { 
    Scanner scan = new Scanner(new BufferedReader(new FileReader(filepath))); 
    while(scan.hasNext()) 
    { 
     String str = scan.nextLine(); 
     FirstChar.add(str.charAt(0)); 
     SecondChar.add(str.charAt(1)); 
     Int.add(Integer.parseInt(str.substring(2,4))); 
     duble.add(Double.parseDouble(str.substring(5))); 
     scan.next(); 
    } 

    for(Integer I:Int) 
     System.out.print(I.intValue()+"\t"); 
    System.out.println(); 

    for(Double d:duble) 
     System.out.print(d.doubleValue()+"\t"); 
    System.out.println(); 

    for(Character Char:FirstChar) 
     System.out.print(Char.charValue()+"\t"); 
    System.out.println(); 

    for(Character Char:SecondChar) 
     System.out.print(Char.charValue()+"\t"); 
    System.out.println(); 
    } 
    catch(Exception e) 
    { 
    e.printStackTrace(); 
    } 
    finally{ 
    scan.close(); 
    } 
    } 
} 
0

考慮您閱讀BufferedReader所有文件和sotred它ArrayArrayList爲我做了mylines:希望下面的代碼將幫助您

void doMyJob(){ 
     String[] mylines=new String[]{ "FH45 36.266","MF18 46.967","MH29 33.309", 
             "MT95 36.983","MR30 23.1768", 
             "FF31 42.55","FF65 39.96", 
             "FT50 30.962","FR80 19.375", 
             "MH36 24.017","QF40 30", 
             "MQ40 30"}; 
     ArrayList<String> data1 = new ArrayList<String>(); 
     ArrayList<String> data2 = new ArrayList<String>(); 
     for(int i=0; i<mylines.length; i++){ 
      //for FIRST two letters 
      data1.add(mylines[i].substring(0,2)); 
      //So on as you want.. 


      //for VALUES 
      String[] raw = mylines[i].split("\\s"); 
      data2.add(raw[1]); 
     } 
     for(int i=0; i<mylines.length; i++){ 
      System.out.println(data1.get(i)); 
     } 
     for(int i=0; i<mylines.length; i++){ 
      System.out.println(data2.get(i)); 
     } 
    } 
0

對於此特定示例,請使用Scanner.nextLine()將整行讀入String對象。

接下來,使用String.charAt(n)的方法,將前兩個字符讀入你的第一和第二場:

String letter1 = lineStringFromFile.charAt(0); 
String letter2 = lineStringFromFile.charAt(0); 

接着,截斷從文件中讀取的行的頭兩個字母:

listStringFromFile = lineStringFromFile.substring(2, lineStringFromFile.length()); 

接下來,創建的字符串掃描儀:

Scanner myScanner = new Scanner(listStringFromFile); 

現在,由於〜應變g包含用空格分隔的兩個數字,可以使用Scanner對象輕鬆地從字符串中讀取下兩個數字。