简体   繁体   中英

Why is my arrays program showing null?

My partner and I are trying to create an array program that separates the first and last name of the array and displays the blood pressure as well as the max, min, SD, and AVG blood pressure. This is our program:

package arrays;
import java.util.*;
import java.io.*;

public class Arrays{
static Scanner input = new Scanner(System.in);
  static double[] BP = {103, 111, 90, 72, 143, 144, 77, 88, 103};
  static double max = BP[0];
  static double min = BP[0];
  static double sd=0;
  static double avg=0;
  static double squareDiff=0;
  static double variance=0;
  static double calculation=0;
  static int i;
  static String item;
  static String[] pname= {"Jimmy Jones", "John Tod", "Tim Smith", "Mary Smith", "Sally Fields", "Smita Parval", "Sukhen Dey", "Jimmy Chang", "JoAnn Todler"};
  static String pfnameArray [] = new String[pname.length];
  static String plnameArray [] = new String[pname.length];
  static String pfname="";
  static String plname= "";
public static void Separate_names() {

for (int i = 0; i < pname.length; i++) {

    if(pname[i].substring(i, i+1).equals(' ')) {
    pfnameArray[i] = pname[i].substring(0, i);
    plnameArray[i] = pname[i].substring(i+1, pname.length+1);
    pfname = pfnameArray[i];
    plnameArray[i]=plname;
}
}
}
public static void Data_Entry() { 
  for (int i = 0; i < BP.length; i++) {
for (int i = 0; i< BP.length; i++) {
     if (BP[i] < min) min = BP[i];
  }
for(int a=0; a<BP.length; a++){
  calculation +=BP[a];
 }


avg=calculation/BP.length;
for(int v=0; v<BP.length; v++){
squareDiff+= Math.pow((BP[v]-avg),2);
variance=squareDiff/BP.length;
}
{sd =Math.sqrt(variance);
} 
}
public static void Print_Data(String [] fName, String [] lName) {
for( int l=0; l<BP.length; l++){

    System.out.println(plnameArray[i] + " " + pfnameArray[i] + ", Blood Pressure: " + BP[l]);
}
System.out.println();
System.out.println("Max is " + max);  
System.out.println("Min is " + min);
System.out.println ("Average is " + avg);
System.out.println("Standard Deviation:" + sd);

}}

For the output of the first and last name it is showing null. How do I separate the Array to where it doesn't show null? Also, is the formula for SD correct? We compared it with someone else's and got a different number.

Use split like this:

pfnameArray[] = pname[i].split(" ");

pfnameArray[0] will contain the first name pfnameArray[1 ] will contain the lase name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM