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

Write a java program (or class) that will manage a collection of integers read f

ID: 3625212 • Letter: W

Question

Write a java program (or class) that will manage a collection of integers read from a file. The file contains unique integers, one per line and all data should be read. Include methods for the following:
- a method that randomly picks a value in the collection, and then creates and returns a new array that "separates" the collection of data around this value;

all the data in the collection less than the randomly chosen value is in the lower part of the returned array and the data greater than or equal to the randomly chosen value is in the upper part of the returned array.
Assume the returned array is full and you do not alter the original collection. The data in the returned array is not yet sorted, just separated with "low" numbers first followed by "high" numbers based on the randomly
chosen value

Here my problem with the code I wrote below is that I dont have the exceptions right, my "separate"method is faulty and my setRandomNum method keeps producing errors. I also have trouble tidying up the readFile method..

import java.util.*;
import java.io.*;
public class Separator
{
   private static int lineCount;
   private static int targetNum;
   private static String fileName = " ";
   private static int [] data= new int[lineCount];
   private static int [] temp = new int[data.length];
  
    public static void main (String[] args) throws FileNotFoundException
    {
       Scanner input = new Scanner(System.in);      
       System.out.println("Enter File Name: ");               
       setFileName(input.next());       
       readFile();
       setRandomNum(data);
       separate(targetNum);
       System.out.println(temp);
    }     
  
    public static String getFileName()
    {
        return fileName;
    }
   
    public static void setFileName(String newFileName)
    {
        if (fileName != newFileName)
        fileName = newFileName;
    }
   
    //my idea here was to find the index of the randomNumber picked and
    //distribute the numbers lower than it to be outputted above the selected random number

    //above or greater to be printed below the randomly selected number
    public static void separate(int targetNumber)
    {  
        int centralPos = 0;
        for (int i = 0; i < data.length; i++)
        {
          if (data [i] == targetNumber)
          centralPos = data[i];   
          else if(data [i] <= targetNumber)
          temp[i] = data[centralPos-1];
          else
          temp[i] = data[centralPos+1];
        }
        for (int i = 0; i < temp.length;i++)
        {
        System.out.println(temp[i]); 
        }
    }
   
    public static void setRandomNum(int [] data)
    {
      Random rand = new Random();  
      targetNum = data[rand.nextInt(data.length)];
     // public int nextInt(int n)
    }
   
    public static void readFile() throws FileNotFoundException
    {
         Scanner input = new Scanner(fileName);
         while(input.hasNext() && input.hasNextLine())        
         {        
          String line = input.nextLine();            
         // Integer.parseInt(line);
          lineCount++;         
         }      
    }      
}

PLEASE COMMENT OUT CORRECTIONS IN CAPS SO I KNOW WHAT I MISSED OUT. THANKS A LOT FOR YOUR TIME IN ADVANCE!!

Explanation / Answer

please rate - thanks

sorry I didn't mark what was changed, because it's a large portion of the code

import java.util.*;
import java.io.*;
public class Separator
{


   private static String fileName = " ";
      public static void main (String[] args) throws FileNotFoundException
    { int targetNum;
       Scanner input = new Scanner(System.in);     
       System.out.println("Enter File Name: ");              
       setFileName(input.next());
                
      int [] data= new int[100];
      int n=readFile(data);
        int [] temp = new int[n];
       targetNum=setRandomNum(data,n);
       separate(targetNum,data,n,temp);
        System.out.println("Target Number "+data[targetNum]);
        System.out.println("Original Separated");
        for(int i=0;i<n;i++)
            System.out.println(data[i]+" "+temp[i]);
    }    

    public static String getFileName()
    {
        return fileName;
    }
  
    public static void setFileName(String newFileName)
    {
        if (fileName != newFileName)
        fileName = newFileName;
    }
  
    //my idea here was to find the index of the randomNumber picked and
    //distribute the numbers lower than it to its left and numbers higher to its right
    public static void separate(int targetNumber,int data[], int n,int temp[])
    { int target=data[targetNumber];
        int i,count=0;
        for(i=0;i<n;i++)
            if(data[i]<target)
                  {count++;
                    }
        temp[count]=target;
        int low=0;
        int high=count+1;
        for(i=0;i<n;i++)
             { if(i!=targetNumber)
                 {                 
                  if(data[i]<target)
                        {temp[low]=data[i];
                           low++;
                         }
                   else
                        {temp[high]=data[i];
                        high++;

                         }
                }
                }
         }
  
    public static int setRandomNum(int data[],int n)
    {int targetNum;
   
      Random rand = new Random();
      targetNum = rand.nextInt(n);
     // public int nextInt(int n)
    return targetNum;
    }
  
    public static int readFile(int data[]) throws FileNotFoundException
    {    int lineCount=0;
         Scanner input=new Scanner(new File(fileName));

         while(input.hasNextInt())       
         {       
          data[lineCount]= input.nextInt();           
         // Integer.parseInt(line);
          lineCount++;  
    
         }  
        return lineCount;  
    }     
}

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