Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with this problem I am trying to solve, I been stuck on it for ho

ID: 667098 • Letter: P

Question

Please help me with this problem I am trying to solve, I been stuck on it for hours:

Problem: To read data from a text file. Structure of the data file: it will have multiple sets of data, the data sets are separated by a blank line. Each data set contains certain number of positive integers.

Task: I want to write a Java program to read a user supplied data file and store each of the data set in separate array. I also want to display the size of each array as output followed by displaying the corresponding data set in sorted order (increasing order of numbers). I want to use Arrays.sort( int[ ] ) method to sort each array.

Any help would be greatly appreciated!!!

Explanation / Answer

/**
* The java program that reads a text file called data.txt
* that contains the integer values separated by blank space.
* Then read each line into an array and print the size
* and sorts the array in ascending order and prits the array.
* */
//DataSet.java
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
public class DataSet
{
   public static void main(String[] args)
   {
      
       //Name of the input file
       String fileName="data.txt";
       //declare a Scanner class
       Scanner filescanner=null;
       //declare a File class
       File file=null;
       //declare an integer type
       int size;
      
       try
       {
           //create a file using File class
           file=new File(fileName);
           //create a Scanner class object with file as argument
           filescanner=new Scanner(file);
          
           while(filescanner.hasNextLine())
           {              
               //read an array of line
               String line=filescanner.nextLine();
               //split the line of integers using split method
               String tokens[]=line.split(" ");
              
               //array of integer type of length of tokens
               int[] array=new int[tokens.length];
              
               //convert string of tokens into integer type using parseInt method
               for (size = 0; size < tokens.length; size++)               
                   array[size]=Integer.parseInt(tokens[size]);
              
               //print the size of the array
               System.out.println("Size of Data set : "+array.length);
                              
               System.out.println("Before sorting");
               //print the elements after sorting
               System.out.println(Arrays.toString(array));
              
               System.out.println("Sorted array in ascending order : ");
               //Call the method sort on class Arrays to sort elemets of array
               Arrays.sort(array);
               //print the elements after sorting
               System.out.println(Arrays.toString(array));
              
           }
          
          
           //close filescanner object
           filescanner.close();
          
       }
       catch (Exception e)
       {
           System.out.println(e);
       }      
   }
}

--------------------------------------------------------------------------------

Sample Input file

data.txt

1 2 4 3 5
2 13 15 16 7 11 21
5 6 7 1 2 3 4

--------------------------------------------------------------------------------

Sample Output:

Size of Data set : 5
Before sorting
[1, 2, 4, 3, 5]
Sorted array in ascending order :
[1, 2, 3, 4, 5]
Size of Data set : 7
Before sorting
[2, 13, 15, 16, 7, 11, 21]
Sorted array in ascending order :
[2, 7, 11, 13, 15, 16, 21]
Size of Data set : 7
Before sorting
[5, 6, 7, 1, 2, 3, 4]
Sorted array in ascending order :
[1, 2, 3, 4, 5, 6, 7]

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote