FaaDoOEngineers.com: Java Experts...Help needed to correct code' Aug 30th 2012, 08:16
FaaDoOEngineers.com India's BIGGEST Website for Engineers & Aspiring Engineers!
Java Experts...Help needed to correct code' Aug 30th 2012, 07:07
I need a java program for my project that reads the contents from one file, replace them and store in new text file...' ' I have text file "filenumber.txt" containing numbers...' The numbers should be replaced with specified character and stored to a new file "filealpha.txt" When I run my code, only string "1" is replaced with character specified...' Rest all remain same...:s' ' I tried a lot but could not succeed...:(whew):' ' Could any1 help me to correct the following java code...:(mm):' (You can also download source code from attachment "Problem_code.rar")' ' '
Code:
/* * Problem: Replace "1" string only instead of all */ import java.io.*; public class Replace { public static void main(String args[]) throws Exception { String str1; String n1 = "1"; String a1 = "A"; String str2; String n2 = "2"; String a2 = "X"; String str3; String n3 = "3"; String a3 = "V"; String str4; String n4 = "4"; String a4 = "I"; String str5; String n5 = "1"; String a5 = "L"; String str6; String n6 = "6"; String a6 = "K"; String str7; String n7 = "7"; String a7 = "N"; String str8; String n8 = "8"; String a8 = "H"; String str9; String n9 = "9"; String a9 = "T"; String str0; String n0 = "0"; String a0 = "H"; String strspace; String nspace = " "; String aspace = "E"; try { FileInputStream fis = new FileInputStream("filenumber.txt"); DataInputStream input = new DataInputStream(fis); FileOutputStream fos = new FileOutputStream("filealphabet.txt"); DataOutputStream output = new DataOutputStream(fos); { /*-------------------1--------------------*/ while (null != ((str1 = input.readLine()))) { int x1 = 0; int y1 = 0; String r1 = ""; while ((x1 = str1.indexOf(n1, y1)) > -1) { r1 += str1.substring(y1, x1); r1 += a1; y1 = x1 + n1.length(); } r1 += str1.substring(y1); str1 = r1; if (str1.indexOf("'',") != -1) { continue; } else { str1 = str1 + "\n"; output.writeBytes(str1); } } /*-------------------2--------------------*/ while (null != ((str2 = input.readLine()))) { int x2 = 0; int y2 = 0; String r2 = ""; while ((x2 = str2.indexOf(n2, y2)) > -1) { r2 += str2.substring(y2, x2); r2 += a2; y2 = x2 + n2.length(); } r2 += str2.substring(y2); str2 = r2; if (str2.indexOf("'',") != -1) { continue; } else { str2 = str2 + "\n"; output.writeBytes(str2); } } /*-------------------3--------------------*/ while (null != ((str3 = input.readLine()))) { int x3 = 0; int y3 = 0; String r3 = ""; while ((x3 = str3.indexOf(n3, y3)) > -1) { r3 += str3.substring(y3, x3); r3 += a3; y3 = x3 + n3.length(); } r3 += str3.substring(y3); str3 = r3; if (str3.indexOf("'',") != -1) { continue; } else { str3 = str3 + "\n"; output.writeBytes(str3); } } /*-------------------4--------------------*/ while (null != ((str4 = input.readLine()))) { int x4 = 0; int y4 = 0; String r4 = ""; while ((x4 = str4.indexOf(n4, y4)) > -1) { r4 += str4.substring(y4, x4); r4 += a4; y4 = x4 + n4.length(); } r4 += str4.substring(y4); str4 = r4; if (str4.indexOf("'',") != -1) { continue; } else { str4 = str4 + "\n"; output.writeBytes(str4); } } /*-------------------5--------------------*/ while (null != ((str5 = input.readLine()))) { int x5 = 0; int y5 = 0; String r5 = ""; while ((x5 = str5.indexOf(n5, y5)) > -1) { r5 += str5.substring(y5, x5); r5 += a5; y5 = x5 + n5.length(); } r5 += str5.substring(y5); str5 = r5; if (str5.indexOf("'',") != -1) { continue; } else { str5 = str5 + "\n"; output.writeBytes(str5); } } /*-------------------6--------------------*/ while (null != ((str6 = input.readLine()))) { int x6 = 0; int y6 = 0; String r6 = ""; while ((x6 = str6.indexOf(n6, y6)) > -1) { r6 += str6.substring(y6, x6); r6 += a6; y6 = x6 + n6.length(); } r6 += str6.substring(y6); str6 = r6; if (str6.indexOf("'',") != -1) { continue; } else { str6 = str6 + "\n"; output.writeBytes(str6); } } /*-------------------7--------------------*/ while (null != ((str7 = input.readLine()))) { int x7 = 0; int y7 = 0; String r7 = ""; while ((x7 = str7.indexOf(n7, y7)) > -1) { r7 += str7.substring(y7, x7); r7 += a7; y7 = x7 + n7.length(); } r7 += str7.substring(y7); str7 = r7; if (str7.indexOf("'',") != -1) { continue; } else { str7 = str7 + "\n"; output.writeBytes(str7); } } /*-------------------8--------------------*/ while (null != ((str8 = input.readLine()))) { int x8 = 0; int y8 = 0; String r8 = ""; while ((x8 = str8.indexOf(n8, y8)) > -1) { r8 += str8.substring(y8, x8); r8 += a8; y8 = x8 + n8.length(); } r8 += str8.substring(y8); str8 = r8; if (str8.indexOf("'',") != -1) { continue; } else { str8 = str8 + "\n"; output.writeBytes(str8); } } /*-------------------9--------------------*/ while (null != ((str9 = input.readLine()))) { int x9 = 0; int y9 = 0; String r9 = ""; while ((x9 = str9.indexOf(n9, y9)) > -1) { r9 += str9.substring(y9, x9); r9 += a9; y9 = x9 + n9.length(); } r9 += str9.substring(y9); str9 = r9; if (str9.indexOf("'',") != -1) { continue; } else { str9 = str9 + "\n"; output.writeBytes(str9); } } /*-------------------0--------------------*/ while (null != ((str0 = input.readLine()))) { int x0 = 0; int y0 = 0; String r0 = ""; while ((x0 = str0.indexOf(n0, y0)) > -1) { r0 += str0.substring(y0, x0); r0 += a0; y0 = x0 + n0.length(); } r0 += str0.substring(y0); str0 = r0; if (str0.indexOf("'',") != -1) { continue; } else { str0 = str0 + "\n"; output.writeBytes(str0); } } /*-------------------space--------------------*/ while (null != ((strspace = input.readLine()))) { int xspace = 0; int yspace = 0; String rspace = ""; while ((xspace = strspace.indexOf(nspace, yspace)) > -1) { rspace += strspace.substring(yspace, xspace); rspace += aspace; yspace = xspace + nspace.length(); } rspace += strspace.substring(yspace); strspace = rspace; if (strspace.indexOf("'',") != -1) { continue; } else { strspace = strspace + "\n"; output.writeBytes(strspace); } } } } catch (IOException ex) { System.err.println("Problem.." + ex); } } }
Attached Files for Direct Download
No comments:
Post a Comment