Re: [問題] 字元陣列轉整數?

看板java作者時間19年前 (2007/03/24 17:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
※ 引述《xelloss86.bbs@ptt.cc (xel)》之銘言: > 我想從A_5X5_10.txt B_5X5_10.txt兩個文件檔裡面讀出資料 轉成整數運算 > 將結果存到C_5X5_10.txt > 可是我不管怎麼寫算出來的答案都是一堆問號... > 查了很多方法寫都沒用 > 文件內容: > 1 2 3 4 5 \R\N > 1 2 3 4 5 \R\N > 1 2 3 4 5 \R\N > 1 2 3 4 5 \R\N > 1 2 3 4 5 \R\N > 1到5是亂數 每個數字之間跟\R中間都有空格 > 以下為部分程式碼... > public static void multiply() throws IOException > { > int x=11; > char a[]=new char [100],b[]=new char [100]; > FileReader fr=new FileReader("A_5X5_10.txt"); > int num=fr.read(a); > fr.close(); > fr=new FileReader("B_5X5_10.txt"); > fr.read(b); > FileWriter fw=new FileWriter("C_5X5_10.txt"); > for (int i=0;i<=num;i++) > { > if((i+1)%2==1&&(i+1)!=x) > { > fw.write((int)a[i]*(int)b[i]); > } > else if((i+1)%2==0&&(i+1)%12!=0) > fw.write(" "); > else if((i+1)%12==0) > fw.write("\r\n"); > else > fw.write(""); > if ((i+1)%12==0) > x=x+12; > } > fr.close(); > fw.close(); > } > 感謝看完 因為剛開始學JAVA 不知道這種問題可不可以拿來問= = > 不行請告知 我在自D public static void multiply() { try { BufferedReader bufferreadera=new BufferedReader( new FileReader("A_5X5_10.txt")); BufferedReader bufferreaderb=new BufferedReader( new FileReader("B_5X5_10.txt")); PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter("C_5X5_10.txt"))); String linea = null; String lineb = null; while ((linea = bufferreadera.readLine()) != null && (lineb = bufferreaderb.readLine()) != null) { String [] a = linea.split("\\s+"); String [] b = lineb.split("\\s+"); for (int i = 0;i<a.length;i++){ out.write(String.valueOf(Integer.parseInt(a[i]) * Integer.parseInt(b[i])) + "\t"); } out.write("\n"); System.out.println("BufferReader : " + linea); } bufferreadera.close(); bufferreaderb.close(); out.close(); }catch(IOException ex){ } } -- oRigiN: BBS(140.116.246.178) bbs.csie.ncku.edu.tw FrOm:dns.fjj.idv.tw
文章代碼(AID): #161F0K00 (java)