• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding Ascii / Binary tokens from .txt files

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Analyze four data files named ascii1.txt, ascii2.txt, binary1.txt and binary2.txt. The ascii1.txt and ascii2.txt are character data such as:
123123 16 999 22 765400 800 600

The binary1.txt and binary2.txt files will also hold integers, but it will hold them in binary form (i.e., four bytes per integer).
Program should work for ANY files containing numbers in the format described above. The files can contain any number of numbers (there could be five, thirty, or seven hundred numbers, etc.) Each file is guaranteed to hold at least one integer.
Process the files in the following manner:
�Open ascii1.txt and add all of the integers together.
�Open the file ascii2.txt and add every other integer to get a sum. The
first number in the file is the first number added to the sum.
�Open the file binary1.txt and sum every other integer. The first number
in the file is the first number added to the sum.
�Open the file binary2.txt and give the sum of the first, last, and middle
elements of the file. Must use random access to get these values. Do not read, or access in any way, any of the other integers in the file other than the first, last and middle integers. Report what these three integers are, and their sum. It is guaranteed that binary2.txt will have an odd number of integers (and at least three integers).
The output of your program should have the following format:
Ascii1 file total is: 890960
Ascii2 file "every other" total is: 890122
Binary1 file "every other" total is: 890960
First, middle and last of ascii2: 123123 + 22 + 600 = 123745
General:
Write ONE program that processes all four files.

Code written so far (didn't investigate randomAccess yet)figure I'll need some while and if statements for each file being analyzed. FileReader line can't be broken and continued on next line or app will not compile.

import java.io.*;
import java.util.*;

public class ProcessFiles
{
public static void main(String args[]) throws IOException
{
FileReader fr1 = new FileReader("E:\\My Documents\\OCC\\
JavaII\\Programs\\ProcessFiles\\dataFiles\\ascii1.txt");

FileReader fr2 = new FileReader("E:\\My Documents\\OCC\\
JavaII\\Programs\\ProcessFiles\\dataFiles\\ascii2.txt");

FileReader fr3 = new FileReader("E:\\My Documents\\OCC\\
JavaII\\Programs\\ProcessFiles\\dataFiles\\binary1.txt");

FileReader fr4 = new FileReader("E:\\My Documents\\OCC\\
JavaII\\Programs\\ProcessFiles\\dataFiles\\binary2.txt");

BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
BufferedReader br3 = new BufferedReader(fr3);
BufferedReader br4 = new BufferedReader(fr4);


String s1 = br1.readLine();
String s2 = br2.readLine();
String s3 = br3.readLine();
String s4 = br4.readLine();

StringTokenizer st1 = new StringTokenizer( s1 );
StringTokenizer st2 = new StringTokenizer( s2 );
StringTokenizer st3 = new StringTokenizer( s3 );
StringTokenizer st4 = new StringTokenizer( s4 );

}
}
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a question?
 
Bill Mackie
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What am I missing?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd solve one file at a time in very small bites. See if you can make this method run by writing something to add a bunch of integers ...

Then try passing "1 2" to get 3, "1 2 3" to get 6. Keep adding more little tests like this until the RunningTotal thingie is solid, then think about reading files.

Your instructor (if you have one) may expect you to write all the string-to-integer stuff yourself. Ask if it's ok to use Scanner from JDK 5 - it can do some of the work for you.

It may be a big of a leap right now, but also look into JUnit.org for a testing tool that helps you write things like this.
 
Bill Mackie
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stan, I will try tonight.....
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You bet. Post what you get and we'll keep playing.
 
Bill Mackie
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't do anything with the above statement, Stan.....This is where I'm at right now. I would be happy if I could get the integers seperated by a space in the particular file to be summed and output to system.out. What gets me is I can't find any references to adding integers stored in a file in my textbook (How To Program Deitel / Deitel)

This is what I'm working with:

import java.io.*;
import java.util.*;
import java.lang.*;

public class ProcessFiles
{
public static void main(String args[]) throws IOException
{
/*FileReader fr1 = new FileReader("E:\\My Documents
\\OCC\\JavaII\\"
+ "Programs\\ProcessFiles\\dataFiles \\ascii1.txt");
BufferedReader br1 = new BufferedReader(fr1);
String s1 = br1.readLine();
StringTokenizer st1 = new StringTokenizer( s1 );


//I know this is wrong, I would like to use valueOf but
// have been unsuccessful...a subtle hint is all I ask.

while (st1.hasMoreTokens())
{
String word = st1.nextToken ();
if (word.length() > 1 )
System.out.println("The sum of all integers
is: " + word);
br1.close();
}

}
}
 
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic