• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to tell whether a number will fit into a particular base.  RSS feed

 
cinmay Xu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need help on java coding homework: chose the base(X=2,3,4,5,6,7,8,9,10)),enter an 8 digit number and let the program to judge if the number is based x(2-10). I already wrote the first two parts and need help on the others.

Don't know how to do these:

[Go over each character of input
If that single character is NOT a valid digit of that base, return false immediately
If you finish going through the characters, return true]

Next is the code:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is generally a good idea to write and test each piece -- as you are building your application. This means that you need to make sure it compiles, even though the program is not complete, so that you may test it... So...

cinmay Xu wrote:Need help on java coding homework: chose the base(X=2,3,4,5,6,7,8,9,10)),enter an 8 digit number and let the program to judge if the number is based x(2-10). I already wrote the first two parts and need help on the others.


Have you tested and confirm that the first two parts work? Does what you have compile?

Henry
 
cinmay Xu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I tested the first two parts and they worked well. I need help from here :
// Go over each character of input
// If that single character is NOT a valid digit of that base, return false immediately
// If you finish going through the characters, return true
I don't know how to compare each char with the base.
Could you give me some tips?



Henry Wong wrote:
It is generally a good idea to write and test each piece -- as you are building your application. This means that you need to make sure it compiles, even though the program is not complete, so that you may test it... So...

cinmay Xu wrote:Need help on java coding homework: chose the base(X=2,3,4,5,6,7,8,9,10)),enter an 8 digit number and let the program to judge if the number is based x(2-10). I already wrote the first two parts and need help on the others.


Have you tested and confirm that the first two parts work? Does what you have compile?

Henry
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well do you what the valid characters are for each base?
Base 2 (binary) 0 and 1
...
Base 10 (decimal) 0 to 9
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Always use them: doesn't it look better. I shall also change the title of the discussion because it is too vague.
I shall try running your code and see what happens.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == true or == false or similar.
Those are not only poor style, but also error‑prone.
Not if (boo == true) ... but if (boo) ...
Not if (boo == false) ... but if (!boo) ...
If you manage to write = instead you will get two errors for the price of one.

You cannot write isBaseX because that is a method. You have to write isBaseX(...)
Your indentation is inconsistent; you have two }s at the end both at the left margin, which might mean you have one } too many. We have some suggestions about indentation. Find a set of formatting convetions you are happy with, and stick to them rigorously. That will make your code easier to read and make errors easier to find, which is a good thing.

You do not need to test the code to see whether you have entered a number; “cinmayXu” is a valid number, but only if your base is 35 or more So you are correct keeping the input as a String.

I would not necessarily have done the exercise the way you have said. Is that how you have been told to do it? If so, then that is how you must do it. Start by working out how you can get a String into its individual chars. Go through the methods of the String class and with any luck you will find something which does exactly what you want.
When you have got that worked out, there are ways to find whether the rest of what you want is correct.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cinmay Xu wrote:Yes, I tested the first two parts and they worked well. I need help from here :
// Go over each character of input
//a bunch more stuff
Could you give me some tips?

My tip would be to do the first thing first. Go over each character. Print each on a single line to be sure you are going over every character.

Until you have that done, don't bother with anything else.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!