作りたいプログラム:
ユーザーが選んだ「名前、名字、残高が書かれたファイル」を配列リストに保存します。
それからCheckWriterというクラスを使って残高をアルファベットで表示するというプログラムを書いています。
例)Colin Emblow $294,648
これを
Colin Emblow $294,648 two hundred ninety four thousand six hundred forty eight
と表示させたいのです。
CheckWriterクラスにArrayListから残高(money)だけを抜き取って、それをintに変換してからparameterとして渡したいのですが、Objectからintに変換できないとエラーが出ます。
また、テキストファル内の残高には”$”が数字の前にあるのでそれがエラーの1つの原因かと思い、splitを使って"$"以降の数字をintに変換したいのですが、どうやらsplitが使えないようで困っています…
このArrayListから残高(money)だけを取り出し、intに変換してCheckWriterクラスのParameterとして渡すにはどうすれば良いでしょうか?
実際に書いたプログラム:
1)Main Program
java
1import java.util.ArrayList; 2import java.beans.FeatureDescriptor; 3import java.io.File; 4import java.io.FileNotFoundException; 5import java.io.FileReader; 6import java.io.FileWriter; 7import java.io.IOException; 8import java.io.PrintWriter; 9import java.util.Scanner; 10import javax.swing.JFileChooser; 11 12public class MainProgram { 13 public static void main(String[] args) throws IOException{ 14 Scanner sc = new Scanner(System.in); 15 16 System.out.println("Choose from the menu below"); 17 System.out.println("Menu:\na) Open a file\nb) Save the file\nc) Exit"); 18 String menu = sc.next(); 19 20 switch(menu.toLowerCase()){ 21 case "a": //Open file 22 JFileChooser jfc = new JFileChooser(); 23 int returnValue = jfc.showOpenDialog(null); 24 if (returnValue == JFileChooser.APPROVE_OPTION){ 25 File selectedFile = jfc.getSelectedFile(); 26 TheFileReader fr = new TheFileReader(); 27 display(fr.readFile(selectedFile)); 28 } 29 break; 30 31 case "b": //Save file 32 JFileChooser jf = new JFileChooser(); 33 jf.setDialogTitle("Specify a file to save"); 34 int userSelection = jf.showSaveDialog(null); 35 36 //writeToFile(userSelection); 37 break; 38 39 case "c": //Terminate program 40 System.out.println("-- End --"); 41 break; 42 43 default: 44 System.out.println("Invalid input."); 45 } 46 } 47 48 private static void display(ArrayList<Employee> list){ 49 CheckWriter cw = new CheckWriter(); 50 String[] sp = new String[list.size()]; 51 for(int i=1; i<list.size(); i++){ 52 String tmp = String.valueOf(list.get(i)); 53 for(int j=0; j<list.size(); j++){ 54 sp[j] = tmp.split("$"); 55 cw.convertToWord(Integer.parseInt(tmp)); 56 } 57 } 58 for(int i=0; i<list.size(); i++){ 59 System.out.printf("%-15s %15s %-15d",list.get(1)); 60 } 61 } 62 63 private static void writeToFile(ArrayList<Employee> list, String[] moneyInWord) throws IOException{ 64 JFileChooser jf = new JFileChooser(); 65 jf.setDialogTitle("Specify a file to save"); 66 int userSelection = jf.showSaveDialog(null); 67 68 if (userSelection == JFileChooser.APPROVE_OPTION) { 69 File fileToSave = jf.getSelectedFile(); 70 System.out.println("Save as file: " + fileToSave.getAbsolutePath()); 71 FileWriter fw = new FileWriter(fileToSave,true); //boolean 72 73 PrintWriter pw = new PrintWriter(fw); 74 for(int i=0; i<list.size(); i++){ 75 pw.println(list.get(i)+" "+moneyInWord[i]+"\n"); 76 pw.close(); 77 fw.close(); 78 } 79 } 80} 81} 82
2)TheFileReader
java
1import java.io.File; 2import java.io.FileNotFoundException; 3import java.io.FileReader; 4import java.util.ArrayList; 5import java.util.List; 6import java.util.Scanner; 7import javax.swing.JFileChooser; 8import javax.swing.filechooser.FileSystemView; 9 10public class TheFileReader { 11 12 public static ArrayList<Employee> readFile(File list) { // I need to return an arrayList, so return al at line 49 13 14// cause my constructor name is Employee 15 ArrayList<Employee> al = new ArrayList<Employee>(); // al = arraylist 16 17 JFileChooser jfc = new JFileChooser(); 18 Employee e;// create an empty employee 19 20 String[] temp; // temp array for storing some data 21 Scanner s; 22 try { 23 s = new Scanner(new FileReader(list));// read a file that was inputed by user. 24 while (s.hasNextLine()) { 25 temp = s.nextLine().split(","); 26 e = new Employee(temp[0],temp[1],temp[2]); 27 al.add(e); // cause I need to input an employee constructor to my arraylist, e is my constructor name 28 } 29 s.close(); 30 31 } catch (FileNotFoundException ee) { 32 System.out.println("error: " + ee); 33 } 34 return al; 35 } 36}
3)CheckWriter
java
1 2public class CheckWriter { 3 4 String[] units = { 5 "", 6 "one", 7 "two", 8 "three", 9 "four", 10 "five", 11 "six", 12 "seven", 13 "eight", 14 "nine", 15 "ten", 16 "eleven", 17 "twelve", 18 "thirteen", 19 "fourteen", 20 "fifteen", 21 "sixteen", 22 "seventeen", 23 "eighteen", 24 "nineteen" 25 }; 26 27 String[] tens = { 28 "", 29 "", 30 "twenty", 31 "thirty", 32 "fourty", 33 "fifty", 34 "sixty", 35 "seventy", 36 "eighty", 37 "ninety", 38 }; 39 40 public String convertToWord(int amount){ 41 42 if (amount < 0) { 43 return "minus " + convertToWord(-amount); 44 } 45 46 if (amount < 20) { 47 return units[amount]; 48 } 49 50 if (amount < 100) { 51 return tens[amount / 10] + ((amount % 10 != 0) ? " " : "") + units[amount % 10]; 52 } 53 54 if (amount < 1000) { 55 return units[amount / 100] + " hundred" + ((amount % 100 != 0) ? " " : "") + convertToWord(amount % 100); 56 } 57 58 if (amount < 1000000) { 59 return convertToWord(amount / 1000) + " thousand" + ((amount % 1000 != 0) ? " " : "") + convertToWord(amount% 1000); 60 } 61 62 if (amount < 1000000000) { 63 return convertToWord(amount/ 1000000) + " million" + ((amount % 1000000 != 0) ? " " : "") + convertToWord(amount % 1000000); 64 } 65 66 return convertToWord(amount / 1000000000) + " billion" + ((amount % 1000000000 != 0) ? " " : "") + convertToWord(amount % 1000000000); 67 } 68 }
4)Employee
java
1 2public class Employee { 3 private String fName; 4 private String lName; 5 private String money; 6 7 public void setFName(String fName){ 8 this.fName = fName; 9 } 10 11 public void setLName(String lName){ 12 this.lName = lName; 13 } 14 15 public void setMoney(String money){ 16 this.money = money; 17 } 18 19 public String getFName(){ 20 return fName; 21 } 22 23 public String getLName(){ 24 return lName; 25 } 26 27 public String getMoney(){ 28 return money; 29 } 30 31 public Employee(){ //default 32 fName = "Dont know"; 33 lName = "Dont know"; 34 money = "0"; 35 } 36 37 public Employee(String m){ 38 money = m; 39 } 40 41 public Employee(String fName,String lName,String money) { // three parameters 42 this.fName = fName; // put user input to your fName in your constructor 43 this.lName = lName; // same as above, but different value here(lName) 44 this.money = money; // same as above, but different value here(money) 45 } 46 47 @Override 48 public String toString() { 49 return String.format( 50 "%s\t%s\t%s\n", fName, lName, money 51 ); 52 } 53 54 55} 56
エラー:
現在このプログラムを実行し、メニューで
a)Open a file
を選択した場合にはこのようなエラーが現れます。
ネットでエラーの意味も検索しましたし、splitやsubstringなどのメソッドも試しましたがうまくいかず困っています。
どうか解決方法がわかるかた、助けていただけませんか?
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー