LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2003, 01:06 PM   #1
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Rep: Reputation: 30
Basic Java arithmetic question


I'm just starting out with Java, but I'm having problems getting the following to compile. I have searched around and tried all sorts of things, but it seems hard to get floats from the args[] array.. Am I doing something badly wrong?

I get the error:

FloatTest.java:10: variable total might not have been initialized
total+=found;
^

----------- code : -----------

import java.lang.*;

class FloatTest{
public static void main(String args[]){
float total;
for(int i=0;i<args.length;i++){
// collect arg and sum to total
float found=Float.valueOf(args[i]).floatValue();
System.out.println("you sent in "+found);
total+=found;
System.out.println("total is now "+total);
}
}
}


thanks,

christo
 
Old 07-05-2003, 01:30 PM   #2
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
fixed by initlaising the variables at the same time as declaring them:


import java.lang.*;

class FloatTest{
public static void main(String args[]){
float total=0.0f;
float found=0.0f;
for(int i=0;i<args.length;i++){
// collect arg and sum to total
found=Float.valueOf(args[i]).floatValue();
System.out.println("you sent in "+found);
total=(total+found);
System.out.println("total is now "+total);
}
}
}


there y' go


christo
 
Old 07-05-2003, 01:32 PM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Firstly, when posting code, wrap the code in [code] [/code] tags. This makes reading the code easier, as it preserves indentation. (=

Secondly, the error is caused by the fact that total was not assigned an initial value.

The code declares total, but later tries to add to it's current value (by +=) without it ever having been given a defined initial value.

If you change
Code:
    float total;
to
Code:
    float total = 0.0F;
that will solve the error.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
basic java programming question nkoplm Programming 3 09-22-2005 01:37 AM
a basic problem in java and linux qyzlp Programming 15 04-17-2005 03:53 AM
A very basic newbie question on Java lmellen Programming 5 07-19-2004 03:59 PM
2 Basic java questions.. Tru_Messiah Programming 6 05-16-2004 01:25 PM
Visual Basic or Java figadiablo Programming 30 08-05-2003 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration