Question

Please help me ONLY for the second method : public static int[] runningGroups(String[] inputFileNames) throws IOException.

The second method has an "array of files as a parameter". and return int [ ]. Each element of the return array is the number of group that can get from the first method.   

I got an error Exception in thread "main" java.lang.NullPointerException

Here my code:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class RunningGroups {

public static void main(String[] args) throws IOException {
  String file = args[0];

  System.out.print(" Total : ");
  System.out.print(runningGroups(file));

            String[] test = new String[5];
            String t1 = args[1] ;
            String t2 = args[2] ;
            String t3 = args[3] ;
            String t4 = args[4];
          
           test[0] = t1;
           test[1] = t2;
           test[2] = t3;
           test[3] = t4;
          
          int[] result = new int[test.length];
          result = runningGroups(test);
           System.out.println("Test array");
           for(int i=0; i<result.length; i++){
              System.out.print(result[i] + " ");
           }

        }

public static int runningGroups(String inputFileName) throws IOException{
    Scanner input = new Scanner(new File(inputFileName));
  String str = "";
  while(input.hasNext()) {
   str = str + input.next() + " ";
  }

  String[] temp = str.split(" ");
  int n = Integer.parseInt(temp[0]);
  int[] start = new int[n];
     int[] speed = new int[n];
     int l = 0,k = 0;
    
     ArrayList<Integer> min_speed_pos = new ArrayList<Integer>();
     int j = 0;
     int min_speed = 1000000;    
    
     for(int i = 1; i<temp.length; i = i+2) {
     start[l] = Integer.parseInt(temp[i]);
     l++;
     }
    
     for(int i = 2; i<temp.length; i= i+2) {   
     speed[k] = Integer.parseInt(temp[i]);
     min_speed = Math.min(min_speed, speed[k]);
     k++;
     }
      
       for(int i = 0; i<n; i++){
          if(speed[i] == min_speed)
             min_speed_pos.add(i);
       }
       return min_speed_pos.size();
       }


public static int[] runningGroups(String[] inputFileName) throws IOException{
int[] groups = new int[inputFileName.length];
  
               for(int i =0; i< inputFileName.length; i++){
                  String temp = inputFileName[i];
                  groups[i] = runningGroups(temp);
               }
  
               
   return groups;
          }
}

Args List: 1in *2.in *3.in *4.in *5.in 6.in 7.in *8.in *9.in 10.in Max Points: 5 Extra credits Description: Create a program named RunningGroups.java and implements the following static method: ubli this method returns the number of running groups tat public static int it returns an int array, each element in the array represents the number of running groups for its corresponding input file s(str inputFileNames) thrONS IOException Then test your program Here is the problem description: There are N (1 < N <= 200,000) people running on a single-lane track. Assuming the track is infinitely long. Each person starts at a distinct position on the track, and runners may run at different speeds. Runners cannot pass each other because there is only one lane in the track. When a faster runner catches up to another person, he or she must slow down to avoid running into the other people, becoming part of the same running group. Eventually, no more runners will run into each other and all runners will run in a group (it is possible there is only one runner in a group), Peter wonders h。ฟ many running groups will be formed eventually. Peter has been working on this problem for more than 3 hours and feels frustrated at this point. Please help this poor guy find this number Input data: The first line of the input file contains the integer N, followed by N lines containing the starting position(>=e) and speed (>0) of each runner. Both numbers(starting position and speed) are at most 1 billion. All runners start at different positions, and these(starting position) will be given in increasing order in the input SANPLE INPUT: 1 2 6 1 The number of running group: 2

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Please help me ONLY for the second method : public static int[] runningGroups(String[] inputFileNames) throws IOException....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • the question, this is not running it throws an exception would you help me it is...

    the question, this is not running it throws an exception would you help me it is a java program // why this is not running would you help me public class sample {     public static void main(String[] args){         String str[][] = new String[10]['K'];                 for(int i= 1;i<=10; i++){             for(char j= 'A';j<='J'; j++){                 System.out.print(i +" "+ j);                  str[i][j] = "A";                   System.out.print(" ");                                                   }              System.out.println();                                  }            ...

  • What is printed by running the following code? public static void main(String[] args) { int[] nums...

    What is printed by running the following code? public static void main(String[] args) { int[] nums = {2, 3, 4}; int n = 5; changeMe1(n, nums); System.out.print( n ); System.out.print(nums[0]); changeMe2(n, nums); System.out.print( n ); System.out.print(nums[0]); } public static void changeMe1(int number, int[] list) { number++; list[0]++; } public static void changeMe2(int number, int[] list) { number = 9; list = new int[1]; list[0] = 99; }

  • package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[]...

    package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[] a) { for ( int i = 0;i<data.length; i++) { if(i %3 == 0) { System.out.print("A"); System.out.print(data[i]); System.out.print(" "); } } I need an explanation of what this code is doing ? is there anything wrong with it }}

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • public class Test { private static int i =0; private static int j =0; public static...

    public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT