• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting Hexadecimal to Decimal and calculating the Average

 
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 { ... }.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Mike Gosling
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*ahem*

*cough-cough*
 
Mike Gosling
Ranch Hand
Posts: 149
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Julian West wrote:
*ahem*

*cough-cough*



Sometimes I hate myself.



The program now works fine. Thanks Julian.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miljan Puletic wrote:
The program now works fine. Thanks Julian.


Are you sure? What about the integer division?
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic