Egert Aia
Egert Aia

Reputation: 469

Using try and catch to get the input

So I was just trying to create a simple program with bit more complicated code. What I was asked to do was write a program which controls whether it's a positive or negative number (I know, it's easy).

What I was trying to do was to catch what was being entered, so if its not a float it will say to use a comma instead of point or enter a number instead of String.

package example1;

import java.util.InputMismatchException;
import java.util.Scanner;

public class numbritest
{
public static void main(String[] args)
{

    float num;

    Scanner sisse1 = new Scanner(System.in);
    try
    {
        System.out.println("Sisesta number:");
        System.out.println("Kaks komakohta on lubatud");
        num = sisse1.nextFloat();
    }

    catch(InputMismatchException exception) //juhul kui sisestab miskit muud
    {
        System.out.println("Kontrolli kas kasutasid koma!");

    }

    if (num < 0) //kui arv väiksem
    {
        System.out.println("Number " +num +" on negatiivne.");
        System.out.println("Seega on arv väiksem nullist");

    }
    else //Kui arv on suurem või võrdne
    {
        System.out.println("Number " +num +" on positiivne.");
        System.out.println("Positiivsed arvud on suuremad");
        System.out.println("või võrdsed nulliga.");

    }
    System.out.println();
    System.out.println("Programm lõpetada!");
  }
  }

Sorry that it's in Estonian, but I hope you get my point.

Upvotes: 0

Views: 90

Answers (4)

sekhar
sekhar

Reputation: 11

Initialize the local varible before use;
In these case compiler consider that try block may execute or not so that num.nextFloat() may execute or not so compiler throws error: variable num might not have been initialized. so that change float num; to float num=0.0f;

Upvotes: 0

Michał Schielmann
Michał Schielmann

Reputation: 1382

What you should do is create a loop and ask user to input number as long as the number is not correct: So this code:

try
{
    System.out.println("Sisesta number:");
    System.out.println("Kaks komakohta on lubatud");
    num = sisse1.nextFloat();
}
catch(InputMismatchException exception) //juhul kui sisestab miskit muud
{
    System.out.println("Kontrolli kas kasutasid koma!");

}

Should be in a loop - for example:

boolean incorrectNumberFormat;
do
{
    incorrectNumberFormat = false;
    try
    {
        System.out.println("Sisesta number:");
        System.out.println("Kaks komakohta on lubatud");
        num = sisse1.nextFloat();
    }

    catch(InputMismatchException exception) //juhul kui sisestab miskit muud
    {
        System.out.println("Kontrolli kas kasutasid koma!");
        sisse1.nextLine();
        incorrectNumberFormat = true;  //repeat the loop;
    } 
} while(incorrectNumberFormat);

By the way - comma won't generate the exception (for me 3,3 doesn't), but 3%3 should give you the exception. The loop can be created in various ways it's just an quick example.

Upvotes: 0

chiastic-security
chiastic-security

Reputation: 20520

I'd change it to

Float num = null;

(note we're now using a Float object rather than float primitive, so it can be null) and then use a while condition:

while (num == null) {
  try {
    //...
    num = sisse1.nextFloat();
  } catch (InputMismatchException e) {
    //give error
  }
}

This will not put anything into num until the num = ... line succeeds, so it'll stay null whenever it fails. This is better than just setting it to 0.0f, because otherwise it'll cause problems if the user enters zero.

Upvotes: 0

user3906612
user3906612

Reputation: 33

Change float num; to float num = 0.0f; and it should work properly

Upvotes: 1

Related Questions