• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Hexadecimal to Decimal and calculating the Average  RSS feed

 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write a program that uses static methods Integer.parseInt and Integer.toString which will convert hexadecimal values into their decimal equivalents and then calculate the average value of those decimal equivalents. The program should read hexadecimal values until the user enters blank space as a sentinal value.

The problem is that I don't know how to deal with x value since it is a string. How to set blank space as some sort of condition ? I have tried to write this program in few different ways, this one is the last version. Also, am I asking myself the "right" question, regarding condition which to set and how to think while solving these kind of assignments ?



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your program looks correct and well-structured. (The indentation in lines 18, 19, 20 is not correcy, but that's just a minor issue).

What is it exactly that you're having problems with? Does the program not do what you expected? Is it the implementation of the check method? Currently it's just checking if the value that you pass it is not an empty string. It's hard to say if that's exactly correct, because I don't know what the readLine method that you use in line 15 returns if you enter "blank space" (if you, for example, just press Enter without entering hexadecimal digits first).

You could write the check method in a shorter way, for example:

The expression !m.equals("") (not m equals empty string) is already a boolean expression, so it's not necessary to wrap this in an if { ... } else { ... }.
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This would work if you were looking for the case where the user presses the Enter key. However, the requirements say if the the user enters a space. In that case you'd need something like this:
 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: What is it exactly that you're having problems with?


Sorry I wasn't specific enough. So, the main problem with this program is that it is not calculating the correct average for some reason. For instance, if I enter hex number FFFF and the next number 1 also as hex, their average in decimal notation should be 32768, but I am getting some completely different value.

Jesper de Jong wrote:  I don't know what the readLine method that you use in line 15 returns if you enter "blank space"


By "blank space" I mean Enter, the user doesn't enter a number or blank space, just hits Enter. When the user hits Enter button the readLine method should "direct" the program to the else part in while loop.


About that equals method, since this is my first time using it.

If I understood this right, the equals method asks whether the object contains the same sequence of characters ? If it does, the the method sends back the result of comparison as true or false.

I am not really sure what this line of code does:



What does this expression returns true orfalse ?

 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*ahem*

*cough-cough*
 
Miljan Puletic
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian West wrote:
*ahem*

*cough-cough*


Sometimes I hate myself.



The program now works fine. Thanks Julian.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you getting the entry? There must be something odd about the ACM packages which allows you to enter an empty String.
By the way: you can use a String method instead of eqlas("").
Why do you think you are converting hex to decimal? You are giving an input in hex, then doing binary arithmetic on it. Basic integer arithmetic is always done in binary. You aren't converting anything form hex to decimal, but you are printing the average in decimal.
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miljan Puletic wrote:
The program now works fine. Thanks Julian.

Are you sure? What about the integer division?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!