Re: why am I getting this error "varible temp1 might not have been initialized"




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.

.



Relevant Pages

  • Testing the following scenarios (of 2 temperatures in Celsius and Fahrenheit)
    ... Temperature and the driver class. ... Using the same float value, I create a second object in unit C and ... char scale3 = strScale.charAt; ...
    (comp.lang.java.programmer)
  • Re: converting strings to numbers..
    ... Here, you're first calling a static method valueOf() from the class Float, ... temporary) Float object you call the method floatValue(), ... An object of Character type contains a single character value. ...
    (comp.lang.java.help)
  • can someone check the code for me.
    ... Temperature and a driver class. ... When I compare 0 Celsius with 25.33 ... Fahrenheit (via constructor with 2 paramenter) using equals, isGreater, ... System.out.println("Enter a float value with 2 decimal position for ...
    (comp.lang.java.programmer)
  • Re: reading csv files
    ... > character, returning no characters to your program. ... char, long, float, float, float, long ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Code Comprehension
    ... three more character to type sure. ... clutter makes things harder to read, so being explicit about things ... The 0.0 is of type double and is coerced to a float, ...
    (comp.programming)