-2

First day learning Java and I came across this problem: I need to divide 2 floats(given from command line input). I am getting an error every time I input my 2 floats. I included my code down below

import java.util.Scanner;

public class doubledivision
{
   public static void main(String [] args)
   {
      float num1, num2;
      
      Scanner in = new Scanner(System.in);
      
      System.out.print("Please enter two floating point numbers: ");
      
      num1 = in.nextInt();
      num2 = in.nextInt();

      float result = num1 / num2;
      System.out.println(result);
   }
}
gerrupLa
  • 1
  • 3
  • 2
    Is it an issue that you're using nextInt() instead of like nextFloat(). Also, what's the error you're getting? – Richard K Yu Jan 30 '22 at 22:08
  • tried it with nextFloat() but still getting the same error – gerrupLa Jan 30 '22 at 22:10
  • once I input my 2 floats eg 4.0 1.0 I get : Exception in thread "main" java.util.InputMismatchException – gerrupLa Jan 30 '22 at 22:12
  • Do you input like 4.0 1.0 in one line or 4.0 then press enter, then 1.0 then press enter again? I'm running your code right now and it is working for me if I press enter in between numbers. Actually I just tried and with the space and it didn't make a difference. I think maybe I'd need more details – Richard K Yu Jan 30 '22 at 22:13
  • I was doing them both in 1 line , I will try your way now :) – gerrupLa Jan 30 '22 at 22:15
  • you probably need `in.nextLine()` between your two `nextInt()` statements. As the Scanner will not read the trailing newline character(s) when using any of the `nextX()` methods – Lino Jan 30 '22 at 22:15
  • same error.. this also comes up after the previous error : at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at doubledivision.main(doubledivision.java:10) – gerrupLa Jan 30 '22 at 22:16
  • I'd try what Lino is saying. I'm not sure why it works for me regardless of including the nextLine() method though. It's a bit strange. When you changed to nextFloat you got this error? I got the InputMismatchException when using nextInt() – Richard K Yu Jan 30 '22 at 22:17
  • yeah I got the same error when I changed it – gerrupLa Jan 30 '22 at 22:23
  • 1
    ok I got it to work , im not sure what was wrong. I started from scratch and it fixed lol – gerrupLa Jan 30 '22 at 22:25
  • Does this answer your question? [Int division: Why is the result of 1/3 == 0?](https://stackoverflow.com/questions/4685450/int-division-why-is-the-result-of-1-3-0) – Alexander Ivanchenko Jan 30 '22 at 23:14
  • 1
    Edit your Question to provide relevant details. Your readers should not have to dredge through the Comments to grasp your Question. – Basil Bourque Jan 30 '22 at 23:16

1 Answers1

1

If you want to read a number of type float from the user, you need to use : in.nextFloat()
because this in.nextInt() will return a value of type int.

Also you need to consider some things :

  • You need to clear the scanner since you want 2 floating numbers , why ? because if the user enters :

    >>> 1.0 2.0
    

    The space will make your scanner assigne num1 with 1.0 and num2 with 2.0. So if you want to ask the user 2 times use :

    num1 = in.nextFloat();
    in.nextLine(); // to refresh
    num2 = in.nextFloat();
    
  • If the user enters zero (any number) divided by 0 will blow up​ your app , so add a check :

    if(num2==0){
      System.out.println("NaN");
    }
    else {
      float result = num1 / num2;
      System.out.println(result);
    }
    
Yasser CHENIK
  • 370
  • 1
  • 6
  • 17