0

My Java Code :

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main{

  static int fun1(double rat[] , double b){
    int c = 0;
    for(int i=0;i<5;i++){
     if(rat[i]>=b)
     c++;
    }
    return c;
  }

  static int fun2( String loc[] ,int est[], String b){
    int c=0,mn=100005,st=-1;
    for(int i=0;i<5;i++){
      if(loc[i].equals(b) && est[i]<mn)
      mn=est[i];
    }
   for(int i=0;i<5;i++){
    if(loc[i].equals(b) && est[i]<mn){
     mn=est[i];
     st = i;
    }
   }
  return st;
  }

public static void main(String args[]){
    int id[] = new int[5] ;
    String name[] = new String[5];
    String loc[] = new String[5];
    int est[] = new int[5];
    double rat[] = new double[5];
    boolean rc[] = new boolean[5];  

    Scanner sc = new Scanner(System.in);

    int i=0;

    while(i<5){
       id[i] = sc.nextInt();
       name[i] = sc.nextLine();
       loc[i] = sc.nextLine();
       est[i] = sc.nextInt();
       rat[i] = sc.nextDouble();
       rc[i] = sc.nextBoolean();
       i++;
    }
    
    String loca = sc.nextLine();
    double rate = sc.nextDouble();

    int bb = fun1(rat,rate);

    if(bb==0)
       System.out.println("No such Museums");
    else
       System.out.println(bb);

    int l = fun2(loc,est,loca);
    if(l==-1){
      System.out.println("No matching museum");
    }
    else{
      System.out.println(name[l]);
      System.out.println(est[l]);
    }

}

}

My code gives this error , I tried my best but I am unable to resolve this error

I tried using scanner.hasNext() but it gives InputMisMatch Exception.

Even if I consume new line left over using scanner.nextLine() , it still gives error as InputMisMatch Exception.

My input for the code :

211
NRM
delhi
1977
4.4
False
456
national museum
delhi
1949
4.6
True
123
government museum
chennai
1851
4.3
True
055
SJM
hyderabad
1951
4.4
True
065
NCM
delhi
1956
4.5
True
delhi
4.5
Tom
  • 14,120
  • 16
  • 41
  • 47
  • 3
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – maloomeister Feb 25 '21 at 15:25
  • @rishabjain if you have to add info to your question please [edit] it. Comments like that are very hard to read. – Federico klez Culloca Feb 25 '21 at 15:25
  • Please post the complete exception stacktrace. – Tom Feb 25 '21 at 15:53

1 Answers1

1

Try to change the while loop to this

while (i < 5) {
      id[i] = Integer.parseInt(sc.nextLine());
      name[i] = sc.nextLine();
      loc[i] = sc.nextLine();
      est[i] = Integer.parseInt(sc.nextLine());
      rat[i] = Double.parseDouble(sc.nextLine());
      rc[i] = Boolean.parseBoolean(sc.nextLine());
      i++;
}
  • It worked but how and why my code was giving error. – rishab jain Feb 25 '21 at 15:53
  • Can you tell the reason why you are taking string as input and parsing it to the following datatype ? Why can't we take input with the respective data-type itself ? – rishab jain Feb 25 '21 at 16:55
  • 1
    You can see, as described in @maloomeister comment's link, "... Scanner.nextXXX method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline". – Roie Shlosberg Feb 26 '21 at 13:09