Re: why am I getting this error "varible temp1 might not have been initialized"
- From: "sandie" <amanda772007@xxxxxxxxx>
- Date: 25 Mar 2006 10:29:00 -0800
Roedy Green wrote:
On 25 Mar 2006 09:42:05 -0800, "sandie" <amanda772007@xxxxxxxxx>
wrote, quoted or indirectly quoted someone who said :
why am I getting this error "varible temp1 might not have been
initialized" and "varible temp2 might not have been initialized"
What if the person puts in a K for the temperature scale for Kelvin.
Then what happens?
The error message was not form the driver class but it has been
solved.
I am about to take care of the issue you raise where a person puts in a
character (or more) other than c,C,f, F or puts no character at all and
no float number at all.
So, in the driver, say a person doesn't enter anything for a float
value or a character for a scale unit. I am supposed to take that
empty value for float (for temperature) and empty string (for scale)
and assign default value of 0 and C respectively using different
constructors. The help I need is the area of
Scanner keyboard1 = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp1 = keyboard1.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale1 = keyboard1.next();
char scale1 = (strScale1.toUpperCase()).charAt(0);
As of now, the program waits for me to enter something. What do I need
to do if the perosn just press enter key w/o putting a float value. And
a string vale? How do I let the program contiue to run so that I can
assing the default value?
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- - - - - - - - -
Here is the whole driver:
/***********************************************************************************************
HW_413_7_driver
Pg 413.7: This program uses "Temperature" class to set compare 2
temperatures values in float.
************************************************************************************************/
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class HW_413_7_driver
{
public static void main( String[] Args)
{
Scanner keyboard1 = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp1 = keyboard1.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale1 = keyboard1.next();
char scale1 = (strScale1.toUpperCase()).charAt(0);
HW_413_7_Temperature t1 = new HW_413_7_Temperature();
System.out.print("First Temperature is ");
System.out.println(t1.toString()); // testing to see the 1st
temperature entered
System.out.println();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp2 = keyboard2.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale2 = keyboard2.next();
char scale2 = (strScale2.toUpperCase()).charAt(0);
HW_413_7_Temperature t2 = new HW_413_7_Temperature(temp2,scale2);
System.out.print("Second Temperature is ");
System.out.println(t2.toString()); //testing to see the 2nd
temperature entered
System.out.println();
if( (t1.equals(t2) )== true)
System.out.println(t1.toString() + " is equal to " + t2.toString()
);
else
System.out.println( t1.toString() + " is NOT equal to " +
t2.toString() );
if( ( t1.isGreater(t2) )== true)
System.out.println(t1.toString() + " is greater than " +
t2.toString() );
else
System.out.println(t1.toString() + " is NOT greater than " +
t2.toString() );
if( (t1.isLess(t2) ) == true)
System.out.println(t1.toString() + " is less than "+ t2.toString()
);
else
System.out.println(t1.toString() + " is NOT less than "+
t2.toString() );
System.out.println();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp3 = keyboard3.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale3 = keyboard3.next();
char scale3 = (strScale3.toUpperCase()).charAt(0);
HW_413_7_Temperature t3 = new HW_413_7_Temperature(temp3,scale3);
System.out.print("Third Temperature is ");
System.out.println(t3.toString());
System.out.println();
if( (t2.equals(t3) )== true)
System.out.println(t2.toString() + " is equal to " + t3.toString()
);
else
System.out.println( t2.toString() + " is NOT equal to " +
t3.toString() );
if( ( t2.isGreater(t3) )== true)
System.out.println(t2.toString() + " is greater than " +
t3.toString() );
else
System.out.println(t2.toString() + " is NOT greater than " +
t3.toString() );
if( (t2.isLess(t3) ) == true)
System.out.println(t2.toString() + " is less than "+ t3.toString()
);
else
System.out.println(t2.toString() + " is NOT less than "+
t3.toString() );
} // end main
}
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
.
- Follow-Ups:
- References:
- Prev by Date: Re: why am I getting this error "varible temp1 might not have been initialized"
- Next by Date: Using Sacnner's nextFloat()
- Previous by thread: Re: why am I getting this error "varible temp1 might not have been initialized"
- Next by thread: Re: why am I getting this error "varible temp1 might not have been initialized"
- Index(es):
Relevant Pages
|