Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 27th, 2008, 12:47 AM
dudenell dudenell is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 6 dudenell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 58 sec
Reputation Power: 0
[Homework] Trying to display name + sum of numbers from file

ok well let me just explain what the program needs to do.

Write a program that will input data from a file and calculate an average. Each line of the file will have a single line followed by series of numbers. calculate the average of the numbers. Write the name and average out to another file...

now here is what I have, this will basically input a file and output the same thing to a different file name..

Code:
import java.io.*;
import java.util.*;

public class FileIOedit
{
   public static void main(String args[])
   {//begin write to file method

      FileReader inFile;
      BufferedReader bFile;
      FileWriter fw;
      BufferedWriter w;
      String s, word, temp = "";
      String inputFile = "Inpu.dat";
      StringTokenizer line;

      try
      {
		 //Open the input stream for reading
         inFile = new FileReader(inputFile);
         bFile = new BufferedReader (inFile);

         //Open the output file for writing
         fw = new FileWriter("test.dat");
         w = new BufferedWriter (fw);

		 //get first line of input file
	     s = bFile.readLine();

		 //while their are more lines in the input file
         while (s != null)
         {
			 line = new StringTokenizer(s);

			 //while there are more words in this line
			 while(line.hasMoreTokens())
			 {
				 word = line.nextToken();

				 //int num = Integer.parseInt(word);

				 //if this word is going to overflow the output line

					temp = temp + word + " ";
			}
			//get next line from the input file
			s = bFile.readLine();
         }
         //write the last line to the output file, flush and close buffer
         w.write(temp);
         w.flush ();
         w.close ();

       }//end try

	   catch(IOException exception)
	   {
			System.out.println("Hey you idiot!!!");
			System.out.println(exception);
	   }
   }//end main method
}//end class


what im trying to do from here is figure out how have the name separate from the actual numbers (I think I need to use strings???) but im confused on how to convert the actual text over to two different data types

im assuming you would use something like this
Code:
	   len = Namebefore.length();
	   position = Namebefore.indexOf(" ");

	   firstName = Namebefore.substring(0, position);
	   lastName = Namebefore.substring(position +1, len);


any help would be greatly appreciated

~Dan~

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > [Homework] Trying to display name + sum of numbers from file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT