Read and count each character in text file and then write the result in other text fi
Sorry, I need help,
I'm newbie in java. I have a task like this : Read and count each character in text file and then write the result in other text file. So called the input.txt contains like this : A=B*(C+D)-15+8*CD then, read the contains input.txt and write the result in other file that named output.txt that explain like this : The Contains of input.txt is : operator : ( total : 1 ) total : 1 * total : 2 + total : 2 - total : 1 / total : 0 = total : 1 variabel : A total : 1 B total : 1 C total : 1 D total : 1 CD total : 1 digit 15 : 1 8 : 1 My Code is like this : import java.io.*; public class compiler_AhmadFadlyDj_2010470121 { public String bacaFile(String namaFile) //To read a txt.file { String returnValue= ""; FileReader file = null; String line = ""; try{ file = new FileReader(namaFile); BufferedReader reader = new BufferedReader(file); while ((line = reader.readLine()) != null) { returnValue += line + "\n"; } }catch(FileNotFoundException apaKek) { throw new RuntimeException("file is not found"); } catch (IOException apaKek){ throw new RuntimeException("IO Error Occured"); } finally{ if(file != null){ try { file.close(); } catch (IOException apaKek) { apaKek.printStackTrace(); } } } return returnValue; } public void TulisFile(String namaFile, String s ) // This is method to write File { FileWriter output = null; try { output = new FileWriter(namaFile); BufferedWriter writer = new BufferedWriter(output); writer.write(s); writer.flush(); writer.close(); } catch(IOException apaKek){ throw new RuntimeException(apaKek); } finally{ if (output != null){ try { output.close(); } catch(IOException apaKek) { apaKek.printStackTrace(); } } } } public static void main(String[] args) { compiler_AhmadFadlyDj_2010470121 test = new compiler_AhmadFadlyDj_2010470121(); String input = test.bacaFile("input.txt"); System.out.println(input); test.TulisFile("output.txt", input); } } So, thanks a lot... |
Firstly, please place your code and data in [code][/code] tags to retain format and make it all clearer to read.
I have not looked at your code as yet, but I have an issue with the question and your current data: Quote:
Code:
operator : You would need to identify why these are to be treated differently as it will alter the code required. |
Reply
Thanks 4 your reply
Actually, I dont understand all about my code. (fresh newbie in java) I just know to read and write the text file... CD is a unitary character (or string perhaps ?), 15 is also counted as a single entity.. I am very confused, almost stress... Can you give me a suggest, what the method that can I use to solve this problem ? Code:
public void TulisFile(String namaFile, String s ) // This is method to write File |
Quote:
Daniel B. Martin |
Reply Again
Please, I 'm indonesian. Sorry 4 my bad english.
I have to write this code just in java... this task is my homework in my college. please, give me a help I'm stuck |
This isn't really related to your question, but please, whatever you do, don't get into the habit of using the word "test" as a variable name. Maybe not so in Java, but in other languages you could run into a nightmare trying to debug an error around that! Speaking from experience.
|
All times are GMT -5. The time now is 01:35 AM. |