LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2013, 08:42 PM   #1
Fadly Massere
LQ Newbie
 
Registered: Nov 2013
Posts: 3

Rep: Reputation: Disabled
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...
 
Old 11-14-2013, 11:36 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 11-15-2013, 12:42 PM   #3
Fadly Massere
LQ Newbie
 
Registered: Nov 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
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

Last edited by Fadly Massere; 11-15-2013 at 12:47 PM.
 
Old 11-15-2013, 02:24 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Fadly Massere View Post
... 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
 
Old 11-15-2013, 08:07 PM   #5
Fadly Massere
LQ Newbie
 
Registered: Nov 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-17-2013, 05:11 PM   #6
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i need to write a c code to ping IP addresses which are read from a text file vinothtitan Linux - Newbie 5 10-12-2014 11:23 PM
[SOLVED] BASH: Write only unique strings to text file (cat or while read question) SilversleevesX Programming 32 08-11-2010 02:24 AM
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 01:38 PM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration