Forums Register Login

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

+Pie Number of slices to send: Send
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:

+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
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

 
+Pie Number of slices to send: Send
Well do you what the valid characters are for each base?
Base 2 (binary) 0 and 1
...
Base 10 (decimal) 0 to 9
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
And then we all jump out and yell "surprise! we got you this tiny ad!"
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 671 times.
Similar Threads
Convert decimal value to binary
Left/Right Padding String with blank spaces or other characters
ISBN checker
Getting wrong answers for conversion
Binary to Decimal conversion
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:04:58.