Java Question..

MillerMan

Newcomer
Alright all!

Antoher question for Uni revision!

Alright I got a bit of code and I want to best if its an interger or not. Ive tried

if(tempInput != int)
{
}

but as I already knew it does not work. What is the logical way to go about this.
 
Well I want to check if somthing that I have inputted is of type double and if it is not to bring up an error message.

ive tried

if(tempInput != instanceof double)
{
System.out.println("Error");
}

but it dont work...
 
You can't use instanceof for primitives. Something like this will work, I think:

Code:
boolean is_dbl;
 
try
{
  Double tmp = new Double(tempInput.doubleValue());
  is_dbl = true;
}
catch(NumberFormatException e)
{
  System.out.println("Not double.");
  is_dbl = false;
}

There is, no doubt, a better way to do it (maybe with autoboxing).
 
Last edited by a moderator:
Java is a statically typed language, and 'double' is not a class (which is why 'instanceof' doesn't work). If you have declared 'tempInput' as a 'double', then it will always be a 'double', and if you have declared it as anything else than a 'double', then it will never be a 'double'.

If you have inputted a string and want to check whether the string is representable as a double, then try something like

boolean canBeRepresentedAsDouble( string S )
{
try { Double.valueOf( S ); }
catch (NumberFormatException e) { return false; }
return true;
}
 
I believe 1/N is always 0 if N is an integer and greater than 1. Assuming this is the case then:

if(1/(N+1) == 0) // integer
else // double
 
this seems dangerous to me, if N == -1 :D (be it a double or a signed int)

what comes to my mind :

if (n==(int)n) // n is a integer

if (n==(double)n) // n is a double

absolutely no guarantees ^^
 
Here my code..... It converts celisus to fahreheit and vise versa
.. Just a little revision project...

import java.util.*;

public class TempReader
{
/**
Computes tempreture in fahrenheit from the celisus tempreture.
@param c tempreture in celisus
@return the tempreture in fahrenheit
*/
public static double celisusToFahrenheit(double c)
{
return((c-32)/9)*5;
}

/**
Computes tempreture in celisus from the fahrenheit tempreture.
@param f tempreture in fahrenheit
@return the tempreture in celisus
*/
public static double fahrenheitToCelisus(double f)
{
return((f*9)/5)+32;
}

public static void main(String[] args)
{
double tempInput;
double cel;
double fah;
String typeInput;

try
{

System.out.println("Hello, Please Enter A Tempreature");
Scanner intKeyboard = new Scanner(System.in);
tempInput = intKeyboard.nextDouble();

if(!(tempInput instanceof Double));
{
throw new InputMismatchException("Error: A Type Other Than A Number Has Been Inputed!");
}


System.out.println("Is The Tempreture In Fahrenheit (f) Or Celsius (c) ?");
Scanner stringKeyboard = new Scanner(System.in);
typeInput = stringKeyboard.nextLine();

if(!(typeInput instanceof String));
{
throw new InputMismatchException("Error: A Type Other Than A String Has Been Inputed!");
}


if(typeInput.trim().toLowerCase().equals("c"))
{
fah = fahrenheitToCelisus(tempInput);
System.out.println("Your Tempreture In Fahrenheit Is: "+fah);
}

else if(typeInput.trim().toLowerCase().equals("f"))
{
cel = celisusToFahrenheit(tempInput);
System.out.println("Your Tempreture In Celsius Is: "+cel);
}

else
{
System.out.println("Error: You Can Only Enter \'C\' Or \'c\' For Celsius Or \'F\' Or \'f\' For Fahrenhiet");
}
}
catch(InputMismatchException e)
{
System.out.println(e.getMessage());
System.out.println("Invaild Type Entered!");
}
}

}
 
Blazkowicz_ said:
this seems dangerous to me, if N == -1 :D (be it a double or a signed int)
True :), but that depends on the specifics of what he's doing. But if that's a potential possibility, something extra like if(N<0) N=-N (or if N can't be changed another if clause checking 1/(N-1)).
 
MillerMan said:
....
tempInput = intKeyboard.nextDouble();

if(!(tempInput instanceof Double));
{
throw new InputMismatchException("Error: A Type Other Than A Number Has Been Inputed!");
}
It's been some time since I've done any java but I think this should work:

try
{
tempInput = intKeyboard.nextDouble();
}
catch (InputMismatchException e)
{
}
 
Back
Top