Can Anyone Help Me Finish My Java Malarky?

Avatar image for get2sammyb
get2sammyb

6686

Forum Posts

1993

Wiki Points

0

Followers

Reviews: 1

User Lists: 4

#1  Edited By get2sammyb

So I feel like SUCH a n00b but I'm fed up of mulling over all this myself when I'm sure there are dudes on this site that can just tell me what I'm doing wrong. I've tried Google so, don't send me that way, I wouldn't be asking if I wasn't stuck.

So as part of the first year of my degree I have to do Java, and, what initially I thought would be easy to get to grips with is actually not turning out so well for me. I'm fine at manipulating already written code, but writing from scratch is like, a nightmare.

Anyway - so I have to make a body mass index calculator as one of the basic exercises (I'm on like Question 6 of 58 *sigh*, hence the thread) -- anyway, I'm having issues with division, so I've created up a little test app to try and work out why division doesn't seem to be working.

Here's my code -

public class test {
    public static void main(String[] args) {
        double divide;
        divide = 183 / 100;
        System.out.println(divide);
           
    }
}

Now we all know right, 183 / 100 is 1.83 -- so why is Java giving me the result "1.0" -- exactly what is wrong with that code?

Really appreciate the (n00b) help in advance.

Avatar image for jayge_
Jayge_

10269

Forum Posts

2045

Wiki Points

0

Followers

Reviews: 1

User Lists: 3

#2  Edited By Jayge_

OCD question, why not just declare "double divide = 183/100;"?

Avatar image for get2sammyb
get2sammyb

6686

Forum Posts

1993

Wiki Points

0

Followers

Reviews: 1

User Lists: 4

#3  Edited By get2sammyb

Mainly because I'm not entirely sure what I'm doing. Either way, does that declaration give the correct answer....

*tries*

Sadly, no. It still tells me the answer is "1.0"; any ideas?

Avatar image for jayge_
Jayge_

10269

Forum Posts

2045

Wiki Points

0

Followers

Reviews: 1

User Lists: 3

#4  Edited By Jayge_
public class Main {
    public static void main(String[] args) {
        double divide = 183.00 / 100.00;
        System.out.println(divide);
            
    }
}

That should work.


Avatar image for get2sammyb
get2sammyb

6686

Forum Posts

1993

Wiki Points

0

Followers

Reviews: 1

User Lists: 4

#5  Edited By get2sammyb

LOL. Haha - that's why I felt awkward about asking, because I knew the solution was going to be something really simple.

That does indeed work. Thank you very much! Seriously, thank you -- I spent 2 hours on that! :(

Avatar image for jayge_
Jayge_

10269

Forum Posts

2045

Wiki Points

0

Followers

Reviews: 1

User Lists: 3

#6  Edited By Jayge_

Word.


Post other ones up if ya need help. These are fun.
Avatar image for get2sammyb
get2sammyb

6686

Forum Posts

1993

Wiki Points

0

Followers

Reviews: 1

User Lists: 4

#7  Edited By get2sammyb
Jayge said:
"Word.

Post other ones up if ya need help. These are fun.
"
I have no doubt I'll be back ;)
Avatar image for godwind
Godwind

2924

Forum Posts

345

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#8  Edited By Godwind

Or you could properly type cast it.  There other methods of doing so.

One way would times it by divide = (1.00*183 )/ (1.00*100);

Another way is divide = (double)183/ (double)100;

The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before.

Avatar image for jayge_
Jayge_

10269

Forum Posts

2045

Wiki Points

0

Followers

Reviews: 1

User Lists: 3

#9  Edited By Jayge_
Godwind said:
"Or you could properly type cast it.  There other methods of doing so.One way would times it by divide = (1.00*183 )/ (1.00*100);Another way is divide = (double)183/ (double)100;The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before."
That's true. I was just showing him how to fix his smaller issue though. It seems he hasn't ran into trouble yet, so who knows.
Avatar image for citizenkane
citizenkane

10894

Forum Posts

29122

Wiki Points

0

Followers

Reviews: 0

User Lists: 106

#10  Edited By citizenkane

I'm taking a Java Programming this semester, too.  I took it in high school, but I need this class for my major (Web Design).

Avatar image for lunarbunny
Lunarbunny

1055

Forum Posts

5590

Wiki Points

0

Followers

Reviews: 2

User Lists: 1

#11  Edited By Lunarbunny

I should do some more programming, maybe get some practice on parsing (I made a semi-functional IRC client in Java but the parsing code was spaghetti)

Avatar image for myke_tuna
myke_tuna

2050

Forum Posts

101

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#12  Edited By myke_tuna

Man, I remember doing stuff like this in my first two years of high school. Yep... forgot almost everything. lol.

Avatar image for citizenkane
citizenkane

10894

Forum Posts

29122

Wiki Points

0

Followers

Reviews: 0

User Lists: 106

#13  Edited By citizenkane
myketuna said:
"Man, I remember doing stuff like this in my first two years of high school. Yep... forgot almost everything. lol."
I forgot most of the Java stuff I learned in high school, but it came back to me pretty quickly in my class this semester.
Avatar image for godwind
Godwind

2924

Forum Posts

345

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#14  Edited By Godwind
Jayge said:
"Godwind said:
"Or you could properly type cast it.  There other methods of doing so.One way would times it by divide = (1.00*183 )/ (1.00*100);Another way is divide = (double)183/ (double)100;The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before."
That's true. I was just showing him how to fix his smaller issue though. It seems he hasn't ran into trouble yet, so who knows.
"
My opinion, always think of the broader case.  Sammy shouldn't be able to just fix a small problem that he came across in problem, he should know how to solve all the problems that come his way.  He should understand the concept that floating decimal point value has a decimal being stored, which is not registered values of an integer.  Since calculations occur right to left, integers will behave as integers, not as doubles to accommodate his problems. 

Avatar image for jayge_
Jayge_

10269

Forum Posts

2045

Wiki Points

0

Followers

Reviews: 1

User Lists: 3

#15  Edited By Jayge_
Godwind said:
"Jayge said:
"Godwind said:
"Or you could properly type cast it.  There other methods of doing so.One way would times it by divide = (1.00*183 )/ (1.00*100);Another way is divide = (double)183/ (double)100;The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before."
That's true. I was just showing him how to fix his smaller issue though. It seems he hasn't ran into trouble yet, so who knows.
"
My opinion, always think of the broader case.  Sammy shouldn't be able to just fix a small problem that he came across in problem, he should know how to solve all the problems that come his way.  He should understand the concept that floating decimal point value has a decimal being stored, which is not registered values of an integer.  Since calculations occur right to left, integers will behave as integers, not as doubles to accommodate his problems.  "
No arguments here.
Avatar image for lunarbunny
Lunarbunny

1055

Forum Posts

5590

Wiki Points

0

Followers

Reviews: 2

User Lists: 1

#16  Edited By Lunarbunny

Just remember that programs always do what you tell them to, even if that isn't what you meant.

Avatar image for ownlyuzinwonhan
OwnlyUzinWonHan

1560

Forum Posts

509

Wiki Points

0

Followers

Reviews: 7

User Lists: 6

#17  Edited By OwnlyUzinWonHan

I thought this was some kind of drink.

Avatar image for zebadee
Zebadee

507

Forum Posts

5

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#18  Edited By Zebadee

I'm more of a C++ guy myself.