2015-12-05 151 views
1

我有一个排的内容.CSV文件:)使用扫描仪读取.CSV文件?

Detroit 25 4 7 W Green Bay 7 7 4 L 

我创建输入

Scanner input = new Scanner(file); //file here is the .csv file 

如果我打印input.next(名新的扫描仪,这是什么显示:

Detroit,25,4,7,W,Green 
Bay,7,7,4,L 

如果我创建一个新的磁盘阵列,并指定一个分隔符,如下所示:

String[] list = input.next().split(","); 

,然后打印阵列

System.out.println(list[2]); 

的索引,我得到这个:

Detroit,25,4,7,W,Green 
7 

而不是4.如何把该.csv的每个值在其自己的逻辑索引? 例子:

list[0] = Detroit list[1] = 25 and so on. 

这是我的全码:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.layout.*; 
import javafx.scene.control.*; 
import javafx.stage.FileChooser; 
import javafx.geometry.*; 
import java.util.*; 
import java.io.*; 

public class POS extends Application 
{ 
    private Button runBtn = new Button("Run"); 
    @Override 
    public void start(Stage stage) 
    { 
     GridPane pane = new GridPane(); 

     VBox vBox = new VBox(20); 
     vBox.setPadding(new Insets(15)); 
     Button selectBtn = new Button("Select File"); 
     selectBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;"); 
     vBox.getChildren().add(selectBtn); 

     selectBtn.setOnAction(e-> 
     { 
     FileChooser fileChooser = new FileChooser(); 
     fileChooser.setTitle("Open Resource File"); 
     FileChooser.ExtensionFilter extFilter = 
         new FileChooser.ExtensionFilter("TEXT files (*.csv)", "*.CSV", ".xlsv", ".XLSV"); 
       fileChooser.getExtensionFilters().add(extFilter); 
     File file = fileChooser.showOpenDialog(stage); 




      run(file); 


     }); 

     RadioButton weekBtn = new RadioButton("Current Week"); 
     RadioButton seasonBtn = new RadioButton("Entire Season"); 

     runBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;"); 



     seasonBtn.setDisable(true); 
     vBox.getChildren().add(weekBtn); 
     vBox.getChildren().add(seasonBtn); 
     vBox.getChildren().add(runBtn); 

     pane.add(vBox, 0, 0); 
     Scene scene = new Scene(pane, 500, 200); 
     stage.setScene(scene); 
     stage.setTitle("POS"); 
     stage.show(); 
    } 
    public void run(File file) 
    { 
     runBtn.setOnAction(e-> 
     { 
     try 
     { 
      Scanner input = new Scanner(file); 
      input.nextLine(); 
      System.out.println(input.next()); 
      sortFile(file, input); 

      input.close(); 
     } 

     catch (InputMismatchException ex) 
     { 
      System.out.println("Error you seem to have typed the wrong type of file"); 
     } 
     catch(IOException ex) 
     { 
      System.out.println("Error, file could not be found"); 
     } 


     }); 
    } 
    public ArrayList<String> sortFile(File file, Scanner input) 
    { 
    String[] list = input.next().split(","); 
    System.out.println(list[2]); 
     if (input.hasNext()) 
     { 
     return null; 
     } 
     return null; 
    } 

} 
+0

可能你正在从'Bay,7,7,4,L'行获得输出。 – Atri

+0

@ElliottFrisch我已经添加了我的完整代码 – Bytes

+0

您的csv文件没有“','”,而您在说'input.next()'打印底特律25,4,7,W,绿色 湾, 7,7,4,L'。这不可能。 – Atri

回答

0

看来,你的CVS文件是制表符分隔格式。你应该使用:

String[] list = input.nextLine().split("\t"); 

它应该工作。

+0

当我用“,”替换“\ t”,然后打印数组的第二个索引,它给了我一个错误,指出索引超出范围 – Bytes

+0

检查是否使用nextLine而不是next。 –

+0

next()和nextLine()给了我相同的结果 – Bytes