Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Read txt file into array and create new output.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Read txt file into array and create new output.

    What I need to do is create a class that creates a text file of an array. (Which I have posted Below)

    What I need help with

    Once that file is created I need to create a class that reads the numbers that were created and then multiplies all the numbers by the number at position [0][0]. Then creates a new .txt file and puts them in there.

    Ex:
    Original numbers
    5 6 3 4

    Output needs to be
    25 30 15 20

    I have a class that reads the numbers and just prints them out. I cant figure out how to read the numbers and do anything with them.


    Code that creates file with array of numbers.....
    import java.io.*;
    import java.util.Random;
    public class Output
    {
     public static void main(String args[])
      {
    	 final int rowW = 2;
    	 final int colH = 4;
     
    	 Random rand = new Random();
    	 int [][] array1 = new int [rowW][colH];
     
    	 for (int row = 0; row < array1.length; row++) {
     
    	     for (int col = 0; col < array1[row].length; col++) {
     
    	             array1[row][col] = rand.nextInt(10);
     
    	          }
     
    	      }
     
      try{
    	  FileWriter  fStream;
    	   PrintWriter  myStream;
    	   fStream = new FileWriter( "randomarray.txt");
    	   myStream = new PrintWriter( fStream );
     
    	   int l= array1.length;
     
    	   for(int i = 0; i < l; i++) {
    	   for(int j = 0; j <= l; j++) {
    	   myStream.print( " "+ array1[i][j] );
    	   }
    	   }
    	   myStream.flush();
    	   myStream.close();
    	}
    	catch (IOException e)  {
    	   System.out.println("I/O exception" );
    	}
    }
     
    }


    Just reads the array and prints it out. I need it to read the numbers and then be able to multiply them and create a new file with multiplied numbers. (I could just add them to the bottom of the original file if that is possible)
     
    import java.io.*;
    class input {
        public static void main(String[] args) {
        	try {
        		   FileReader  fReader;
        		   BufferedReader  reader;
        		   int  charAsInt;
        		   fReader = new FileReader("randomarray.txt");
        		   reader = new BufferedReader( fReader );
        		   charAsInt = reader.read();
        		   while ( charAsInt != -1 )   {
        		      System.out.print( (char)charAsInt); 
        		      charAsInt = reader.read();
        		   }
        		   reader.close();
        		}
        		catch (IOException e)  {
        		   System.out.println( "I/O exception" );
        		}
     
     
     
     
        }
    }

    Thanks in advance for any help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read txt file into array and create new output.

    how to read the numbers and do anything with them.
    The Scanner class has methods that will read from a text file and convert the String that was read to an int or double value.
    The read() method reads bytes from the file and will require you to do some work to convert those bytes back to the number. The Scanner class methods will be much easier to use.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: February 21st, 2012, 01:25 AM
  2. Replies: 0
    Last Post: June 19th, 2011, 02:16 AM
  3. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  4. Read in file and store in 2D array start of The Game of Life
    By shipwills in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 09:52 AM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM

Tags for this Thread