• Post Reply Bookmark Topic Watch Topic
  • New Topic

14-Based Numbering System  RSS feed

 
Andrew Tran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Currently, I am trying to write a program where it would ask the user for a 14-based number and it would shoot out a 14-based number. The prompt is as follows...
"An alien species uses 14-based numbering system. Their ten digits, 0 through 9, are the same as our
decimal system. They use A, J, Q and K to represent decimal 10, 11, 12 and 13, respectively. They
hire you to write a Java program to do the summation of their two numbers.
The program should prompt users for two 14-based numbers and then display the summation of those
two numbers. The output should also be 14-based. (They are unwilling to learn our decimal systems!)
For example, if inputs are 17 and 96, the output should be AK."

when i enter 17 and 96, it shoots out AK, which is what i wanted. When i enter something like 1Z, it would pop up the "Your first/second input is invalid," which is also what is expected. But When i input something like 1A, or j1, it would give me the same error " Your first/second input is invalid" , though it should go through. I feel like i did something wrong in the While loop, but im not quite sure. Any help would be greatly appreciated.
Thanks,



OUTPUT
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Tran wrote:when i enter 17 and 96, it shoots out AK, which is what i wanted. When i enter something like 1Z, it would pop up the "Your first/second input is invalid," which is also what is expected. But When i input something like 1A, or j1, it would give me the same error " Your first/second input is invalid" , though it should go through. I feel like i did something wrong in the While loop, but im not quite sure. Any help would be greatly appreciated.

Question 1: Why (or when) does your program print out those messages? Hint: it has nothing to do with your while loop.
Question 2: Why (or when) does it print out both messages? (you have two of them)

A couple of other points for you:
1. You seem to have an awful lot of code for converting to/from base-14.
Suppose you had the following String in your Alien class:
  private static final String DIGITS = "0123456789AJQK";
How do you think that might help you?

(Hint: have a look at String.charAt(index) and String.indexOf(ch))

2. Be careful about case. You input "j1", but your validate method doesn't check for 'j'.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am sure there must be a better way to convert the numbers from Alien to decimal than all those ifs. Have you come across the switch statement? There must also be a better way to convert number from decimal to Alien than those ifs. I shall let you work out what I am thinking of...
 
Andrew Tran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hmm thats what i am trying to figure it out. Im suspecting that it could be because it is case sensitive. I added the 'a', 'j' 'q' and 'k' into the validateinpute method so that its able to also recognize lower case when the user input a letter. would the problem lies in the validateinput method? It seems like the calculation and conversion is fine, but i feel like it went wrong somewhere in the validation method. Pretty stunt as to why it would print out the "Your input is invalid," since if it isnt any of those values then it would be returned as false.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This expression is always true regardless of what currentChar is. For example suppose it's 'A'. Then it's equal to 'A' but not equal to 'J' and so the expression is the same as , which is true. Likewise if it's 'C' then the expression is the same as , which is also true.

Your logic requires that the current character isn't 'A' and isn't 'J' and isn't any of those other things.
 
Andrew Tran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

This expression is always true regardless of what currentChar is. For example suppose it's 'A'. Then it's equal to 'A' but not equal to 'J' and so the expression is the same as , which is true. Likewise if it's 'C' then the expression is the same as , which is also true.

Your logic requires that the current character isn't 'A' and isn't 'J' and isn't any of those other things.



oh that makes sense. Thanks for the help =]
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Tran wrote:hmm thats what i am trying to figure it out. Im suspecting that it could be because it is case sensitive. I added the 'a', 'j' 'q' and 'k' into the validateinpute method so that its able to also recognize lower case when the user input a letter. would the problem lies in the validateinput method?

Yes, but adding lowercase letters is a clumsy way to solve it. Why not just use toUpperCase() like you do elsewhere? In fact, why not just convert whatever your user enters to upper case before you do anything with it?

Also: did you think about my suggestion of a DIGITS String? Because then you could do something like:
Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!