For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2006 > Re: why am I getting this error "varible temp1 might not have been initialized&q









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2006-03-25, 10:02 pm


Roedy Green wrote:
> On 25 Mar 2006 09:42:05 -0800, "sandie" <amanda772007@yahoo.com>
> wrote, quoted or indirectly quoted someone who said :
>
>
> 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.


Roedy Green

2006-03-25, 10:02 pm

On 25 Mar 2006 10:29:00 -0800, "sandie" <amanda772007@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>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?


What happens when you do that? Do you get 0, a null, a empty string,
an exception? Find out. Then put in code to detect that condition and
either issue and error message and repeat the question or assume some
default value.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green

2006-03-25, 10:02 pm

On 25 Mar 2006 13:14:31 -0800, "sandie" <amanda772007@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>How to find out when the program doesn't continue? Using the debugger?
> Is there a simpler approach at this stage?


You just dump values to System.out to indicate progress. Or you can
use a tracing debugger like the one in Eclipse or IntelliJ.
j
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green

2006-03-25, 10:02 pm

On 25 Mar 2006 13:24:12 -0800, "sandie" <amanda772007@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>I did the following and the program compiled but when exectute, it sits
>and wait for input.


What did you expect? You said wait until the user gives an input, so
it does. It will wait patiently like the robot in the parking garage
at the restaurant at the end of the universe with the brain the size
of a planet. (A Douglas Adams reference).

If you want it to give up after X seconds, you need some sort of
timer. I strongly doubt that is what you prof wanted, however.
see http://mindprod.com/jgloss/timer.html

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green

2006-03-25, 10:02 pm

On 25 Mar 2006 15:51:03 -0800, "sandie" <amanda772007@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>This is online class and the assignment is out of the book using what
>we have learned up to th is point. But if I use a timer, he wouldn't
>mind. I will have to figure out how to use it but there must be a way
>to do with what we have learned from the book up to this point. Of
>course, it would be nice to learn extra but time is tight and so I am
>going to have to take care of some imprtant non-school related things
>before coming back to this.

You could do it with a separate thread that slept, if you have
studied threads. I would read the question carefully. I am pretty you
prof intends the program to be infinitely patient. That is just the
way computer programs traditionally behave. I could count on one hand
the ones I have seen that impatiently make an assumption if I don't
act in time. They were bank machines and telephone robots. I don't
think I have ever seen a desktop application that did that.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com