2016-08-03 142 views
1
public class ScriptCreator { 

    public static void main(String[] args) throws IOException { 
     #Choose the CSV file that I am importing the data from 
     String fName = "C:\\Users\\MyUser\\Downloads\\CurrentApplications (1).csv"; 
     String thisLine; 
     int count = 0; 
     FileInputStream fis = new FileInputStream(fName); 
     DataInputStream myInput = new DataInputStream(fis); 
     int i = 0; 
     #Prints the List of names in the CSV file 
     while((thisLine = myInput.readLine()) != null){ 
      String strar[] = thisLine.split(","); 
      Printer(strar[0]); 
     } 

    } 

    public static void Printer(String arg) throws IOException{  
     #Want to pull from the String strar[0] from above 
     #Says that it cannot be resolved to a variable  
      String name = arg; 
      String direc = "C:/Users/MyUser/Documents/"; 
      String path = "C:/Users/MyUser/Documents"; 
      Iterable<String> lines = Arrays.asList("LOGIN -acceptssl ServerName","N " + name + " " + direc ,"cd " + name,"import " + path + "*.ppf" + " true","scan", "publishassessase -aseapplication " + name,"removeassess *","del " + name); 
      Path file = Paths.get(name + ".txt"); 
      Files.write(file, lines, Charset.forName("UTF-8")); 

    } 

} 

大家好,並且預先感謝您提供任何幫助,您可能會給我。我正在嘗試創建一個java程序,它將從CSV文件中提取名稱,並將這些名稱用於爲文本文件生成自定義輸出。我很難設置一個變量,我可以使用它來獲取正在打印的名稱,並使用它們通過設置名稱變量來生成文本文件。 我也需要一些幫助,以確保它爲CSV文件中的名稱量創建腳本的數量。防爆。 CSV中的7個名稱會生成7個自定義.txt文件,每個文件都有其適當的名稱。用java生成自定義文本文件

任何幫助,非常感謝!

編輯:我已更新我的代碼以匹配使代碼正常工作所需的更正。

回答

0

看起來你有一些範圍界定問題。每當你聲明一個變量時,它只存在於最接近的一組大括號的邊界內。通過在main方法中聲明strar,唯一可以明確使用它的地方就在你的main方法中。你的Printer()方法之前沒有提到過strar,它可以知道的唯一方法是將它作爲參數傳遞給函數。

Printer(String[] args) 

或者,更好:

Printer(String arg) 

,然後用

Printer(strar[0]); 

也稱它在你的while循環,你的打印機的方法開始於「爲每個「循環調用strar [0],這不是一個foreach循環的有效目標,因爲如果我記得正確,字符串是不是一個Iterable對象。如果您按照我的建議實施了打印機功能,則無論如何您都不需要每個循環,因爲一次只能傳遞一個名稱。

+1

你恰好是我現在最喜歡的人之一。有效!!我試圖投票,但我沒有足夠的聲譽。 – Dom

+0

不用擔心!很高興我能幫上忙 :) – Daniel