-1

I am new to java and my teacher asks me to write a java program about getting the tallest person and the shortest person and their bmi out from a list of input. The input is like(the fist is the number of student and the second number is the height:

4
Diamond 178 55
Jarod 160 80
Douglas 180 60
Rod 151 48

import java.util.*;

public class Measurement {

 public static void main(String[] args) {
   int n,weight;
   double height,tallh,shorh,bmis,bmit;
   String nameh,names;
   Scanner sc = new Scanner(System.in);
   n=sc.nextInt();
   String[][] arr= new String[n][3];
   for(int i=0;i<n;i++){
     String str = sc.nextLine();
     String[] s = str.split(" ");
     arr[i][0]=s[0];
     arr[i][1]=s[1];
     arr[i][2]=s[2];
   } 
   tallh=Double.parseDouble(arr[0][1]);
   shorh=Double.parseDouble(arr[0][1]);
   bmit=0;
   bmis=0;
   nameh="";
   names="";
   for(int w=0;w<n;w++){
     if(tallh<Double.parseDouble(arr[w][1])){
       nameh=arr[w][0];
       tallh=Double.parseDouble(arr[w][1]);
       bmit=Double.parseDouble(arr[w][2])/Math.pow(tallh/100,2);
     }
     if(shorh>Double.parseDouble(arr[w][1])){
       names=arr[w][0];
       shorh=Double.parseDouble(arr[w][1]);
       bmis=Double.parseDouble(arr[w][2])/Math.pow(shorh/100,2);
     }
   }
   System.out.printf(names+"is the shortest with BMI equals to %.2f/d",bmis);
   System.out.printf(nameh+"is the tallest with BMI equals to %.2f/d",bmit);
 }
}

I dont know why I get this error?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Ellie.P
  • 1
  • 1
  • 1
    The above linked question is generally about `ArrayIndexOutOfBoundsException` and [this question](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) is about your real problem. – Tom Jan 25 '16 at 03:06
  • @Tom: I knew that there were dups, but couldn't quickly find them -- hence I answered with a community wiki. – Hovercraft Full Of Eels Jan 25 '16 at 03:08
  • 1
    @HovercraftFullOfEels I hope don't use the search on this side, because it is hard to find what you're looking for there :D. You google (or a similar search engine) instead. It works far better and often has StackOverflow in the first 3 hits. – Tom Jan 25 '16 at 03:12

1 Answers1

0

You're not handling the end of line token correctly for the first line. To fix this, simply change

n=sc.nextInt(); // leaves a dangling end of line token

to:

n=sc.nextInt();
sc.nextLine(); // swallow the end of line token

This will swallow the token and should set things to right.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373