Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Problem with String Variable

 
Thread Tools Search this Thread
Old 12-04-2005, 07:15 AM   #1
Default Problem with String Variable


As you can probably guess by the title, I'm a newbie. I'm still hitting
brick walls on with really easy solutions. One of them is on the latest
excersices in my textbook. I've written and compiled my program, but I
get this error. Any help/advice would be greatly appreciated.
variable act may not have been initialized

import java.util.*;

public class activities
{
public static void main(String[] args)
{
String act;
int temp;
Scanner keyboard= new Scanner(System.in);
System.out.println("In order to decide what is the best activity at
the present time, please enter the current temperature ");
temp = keyboard.nextInt();

if (temp > 85)
act = "Swimming";
else if ((temp > 70) && (temp <= 85))
act = "Tennis";
else if ((temp > 32) && (temp <= 70))
act = "Golf";
else if ((temp > 0) && (temp <= 32))
act = "Skiing";
else if (temp <=0 )
act = "Dancing";
System.out.println("The recommened activity for the current
temperature is " );
System.out.println(act);




}
}



machoextreme
  Reply With Quote
Old 12-04-2005, 07:31 AM   #2
di.dongke@163.com
 
Posts: n/a
Default Re: Problem with String Variable
variable act can be initialized like this:
String act = null;



di.dongke@163.com
  Reply With Quote
Old 12-04-2005, 07:41 AM   #3
machoextreme
 
Posts: n/a
Default Re: Problem with String Variable
Thanks alot. As I said, so far for me it's usually something really
stupid. The program works perfectly now.



machoextreme
  Reply With Quote
Old 12-04-2005, 07:41 AM   #4
IchBin
 
Posts: n/a
Default Re: Problem with String Variable
machoextreme wrote:
> As you can probably guess by the title, I'm a newbie. I'm still hitting
> brick walls on with really easy solutions. One of them is on the latest
> excersices in my textbook. I've written and compiled my program, but I
> get this error. Any help/advice would be greatly appreciated.
> variable act may not have been initialized


[snip]

> String act;

Could change this to
String act="";
[snip]

--


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)


IchBin
  Reply With Quote
Old 12-04-2005, 03:39 PM   #5
Chris Smith
 
Posts: n/a
Default Re: Problem with String Variable
machoextreme <> wrote:
> variable act may not have been initialized
>


You've gotten a few responses that will work, but none that is a good
idea. Both responses suggested initializing the variable to a nonsense
value, null or "", which the variable should never actually hold. It's
not a good idea to get into the habit of initializing local variables to
nonsense values in Java. By doing so, all you do is prevent the
compiler from telling you when you really do forget to initialize
something to a sensible value.

The problem is that you don't have a final else clause in your if/else
chain. It turns out that you happen to cover all possibilities with if
statements, but that's rather fragile. There are two choices you could
make:

(1) Remove the last if statement, and make dancing a default:

if (temp > 85) act = "Swimming";
else if ((temp > 70) && (temp <= 85)) act = "Tennis";
else if ((temp > 32) && (temp <= 70)) act = "Golf";
else if ((temp > 0) && (temp <= 32)) act = "Skiing";
else act = "Dancing";

(2) Add an explicit else and throw an exception:

if (temp > 85) act = "Swimming";
else if ((temp > 70) && (temp <= 85)) act = "Tennis";
else if ((temp > 32) && (temp <= 70)) act = "Golf";
else if ((temp > 0) && (temp <= 32)) act = "Skiing";
else if (temp <= 0) act = "Dancing";
else throw new RuntimeException("Forgot to specify this acitivity");

If dancing is a sensible default, then (1) causes your application to
continue working even if you mess up a temperature range. If not, then
(2) at least guarantees that your application fails reliably and
consistently when you've broken things. In either case, you aren't
lying to the compiler.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Chris Smith
  Reply With Quote
Old 12-04-2005, 04:14 PM   #6
Thomas Hawtin
 
Posts: n/a
Default Re: Problem with String Variable
machoextreme wrote:
> I've written and compiled my program, but I
> get this error.


I would take a slightly different approach to the other responders to
this thread. I would move declarations down to where they are first
needed. There is a path, which with a bit of effort looks as if in
practice that it is not possible, where act isn't assignable. Putting an
assignment there would suppress the error.

> String act;
> int temp;


Remove these two lines.

> Scanner keyboard= new Scanner(System.in);
> System.out.println("In order to decide what is the best activity at
> the present time, please enter the current temperature ");
> temp = keyboard.nextInt();


Make this
int temp = keyboard.nextInt();

The variable name looks as if it is just saying "temporary". A better
name would be something like temperatureFahrenheit. Outside of the US
Celsius is more common. Kelvin is often used in science and engineering
areas. Also "action" is probably better than "act".

We will need to declare act here. If we declare it final, then we can be
sure that there will be exactly one assignment.

final String act;

> if (temp > 85)
> act = "Swimming";
> else if ((temp > 70) && (temp <= 85))
> act = "Tennis";
> else if ((temp > 32) && (temp <= 70))
> act = "Golf";
> else if ((temp > 0) && (temp <= 32))
> act = "Skiing";
> else if (temp <=0 )
> act = "Dancing";


If we slide in a further else statement, we can ensure that act is
"definitely assigned". There is a whole chapter on definite assignment
in the Java Language Specification, which isn't quite as dull as it
sounds. Essentially a variable is definitely assigned if all execution
paths definitely assign it, but the compiler does not check for tautologies.

So:

else {
act = "Unknown";
}

Alternatively we could indicate that reaching that point is an error, with:

else {
throw new Error(
"Unexpected execution path (temp="+temp+")"
);
}

Rather than redundantly checking each range, a better way to state this
particular logic is with something like:

String act =
(temp <= 0) ? "Dancing" :
(temp <= 32) ? "Skiing" :
(temp <= 70) ? "Golf" :
(temp <= 85) ? "Tennis" :
"Swimming";

The parentheses aren't strictly speaking necessary but, as inequalities
are not common (unlike ==null) in this position, I feel it worthwhile.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/


Thomas Hawtin
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
Java String Problems rbnbenjamin General Help Related Topics 0 02-03-2009 11:02 PM
Passing value with out using variable in query string in PHP! Ali_ggl General Help Related Topics 0 11-29-2008 12:22 PM
Hidden linebreaks in string? VB.NET Jiggy Software 0 04-23-2008 02:18 PM
Re: Serious Computer Problem hootnholler A+ Certification 1 11-24-2003 12:18 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46