Read and count each character in text file and then write the result in other text fi
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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();
}
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.
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
Last edited by Fadly Massere; 11-15-2013 at 12:47 PM.
... 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.