Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Dark

Thread: Writing and Reading Data from a file without BufferedReader

  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Writing and Reading Data from a file without BufferedReader

    Write a program to creat a file named "Exercise9_19.txt" if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read the data back from the file and display the sorted data.

    Here is my code:
    Java Code:
    import java.io.*;
    import java.util.Random;
    import java.util.*;
    
    public class WritingReadingData {
    	public static void main(String[] args) throws Exception {
    		java.io.File file = new java.io.File("Exercise9_19.txt");
    		if(file.exists()) {
    			System.out.println("File already exists");
    			System.exit(0);
    		}
    		java.io.PrintWriter output = new java.io.PrintWriter(file);
    		Random numbers = new Random();
    		for (int i = 0; i < 100; i++) {
    			int num = numbers.nextInt();
    			output.println(num + " ");
    		}
    		output.close();
    		java.io.File readFile = new java.io.File("Exercise9_19.txt");
    		java.util.Scanner input = new java.util.Scanner(readFile);
    		while (input.hasNext()) {
    			String randomNumbers = input.next();
    			System.out.println(randomNumbers + " ");
    		}	
    		input.close(); 
    	}
    	public static void selectionSort(double[] list) { 
    		for (int i = list.length - 1; i >= 1; i--) {  
    			double currentMax = list[0]; 
    			int currentMaxIndex = 0; 
    			for (int j = 1; j <= i; j++) { 
    				if (currentMax < list[j]) { 
    					currentMax = list[j]; 
    					currentMaxIndex = j; 
    				} 
    			} 
     			if (currentMaxIndex != i) { 
    				list[currentMaxIndex] = list[i]; 
    				list[i] = currentMax; 
    			} 
    		} 
    	}
    }
    My code compiles and runs. However it does not display the numbers sorted. The selection sort method in my program isn't used because I'm not sure how I would use this method to sort data from a file. My instructor asked that we do not use BufferedReader in our program. How can I sort the data read from a file?

  2. #2
    ngosti2000 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Writing and Reading Data from a file without BufferedReader

    The Code will not run because everytime your program closes when it gets the file.
    I have modified the code for you.I guese from the code you should be able to know where you made mistakes and can also check out my comments
    Java Code:
    public class WritingReadingData {
        public static void main(String[] args) throws Exception {
            java.io.File file = new java.io.File("c:\\Exercise9_19.txt");
            int nums[]=new int[100];
            int index=0;
            if(file.exists()) 
            {
                
                System.out.println("File already exists");
                
                //System.exit(0);
                //if the file exists read its contents dont exit the program
               
                java.io.File readFile = new java.io.File("c:\\Exercise9_19.txt");
                java.util.Scanner input = new java.util.Scanner(readFile);
            
                while (input.hasNext()) 
                {
                nums[index]= input.nextInt();
                
                 
                index+=1;
                } 
                input.close();
                swap(nums);
                
               
            
            }
            else
            {
                
                java.io.PrintWriter output = new java.io.PrintWriter(file);
                Random numbers = new Random();
                for (int i = 0; i < 100; i++) 
                {
                //get the int from Random Class
                int num = numbers.nextInt();
                //use out.print rather println as println prints each integer in a new line
                output.print(num+" ");
            }
            output.close();
            }
            
            
            
        }
        
        public static void swap(int [] values)
        {
            int temp;
            
            
              for(int i=0;i<values.length;i++)
              {                   
               for(int k=1;k<values.length-1;k++)
               {
                   if(values[k-1]>values[k])
                   {
                      temp=values[k-1];
                      values[k-1]=values[k];
                      values[k]=temp;
                   }
                    
               }
              
              }
            
            for(int i=0;i<values.length;i++)
            {
                System.out.println(values[i]);
            }
            
        }
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Writing and Reading Data from a file without BufferedReader

    Ooh look, spoon feeding.
    Dark likes this.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Writing and Reading Data from a file without BufferedReader

    @ngosti2000, by you "modifying" his code you have only hurt the OP in their endeavor to learn. They still will not know why theirs didn't work, what makes yours work, or how to do it later in their ventures. We don't spoon feed here, we simply usher posters in the right direction so they can learn what they are lacking in.
    ngosti2000 likes this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    ngosti2000 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Writing and Reading Data from a file without BufferedReader

    point pocketed

  6. #6
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Writing and Reading Data from a file without BufferedReader

    @ngosti2000 Thanks. Even though it was spoon feeding, I appreciate the help.

    @Dark I will study ngosti2000's code and try to figure out why his code worked when mine didn't.

Similar Threads

  1. Writing and Reading data to and from xml file to JTable
    By ramihemang90 in forum Advanced Java
    Replies: 3
    Last Post: 03-15-2011, 02:07 PM
  2. Reading part of file with BufferedReader
    By mattgavin in forum New To Java
    Replies: 2
    Last Post: 11-13-2010, 02:44 AM
  3. Reading a file from Applet (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-22-2008, 10:51 PM
  4. Reading file contents (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •