LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read and count each character in text file and then write the result in other text fi (https://www.linuxquestions.org/questions/programming-9/read-and-count-each-character-in-text-file-and-then-write-the-result-in-other-text-fi-4175484630/)

Fadly Massere 11-14-2013 08:42 PM

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...

grail 11-14-2013 11:36 PM

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:

Read and count each character in text file and then write the result in other text file.
Your question quite clearly states 'character', however in your table of what is contained in input.txt you have the following:
Code:

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

The 2 items highlighted in red are not characters, ie 15 has 2 characters in and so does CD

You would need to identify why these are to be treated differently as it will alter the code required.

Fadly Massere 11-15-2013 12:42 PM

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
{
FileWriter output = null;
try
{
output = new FileWriter(namaFile);
BufferedWriter writer = new BufferedWriter(output);       
writer.write(s);
writer.flush();
writer.close();
}

thanks

danielbmartin 11-15-2013 02:24 PM

Quote:

Originally Posted by Fadly Massere (Post 5064678)
... I'm newbie in java. I have a task like this ...

You have a task. You are a newbie in Java. Are these statements tightly coupled? Are we to understand the task must be done in Java? It could be done in other languages faster, easier, and with fewer lines of code.

Daniel B. Martin

Fadly Massere 11-15-2013 08:07 PM

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

devnull10 11-17-2013 05:11 PM

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.