Peter Shipway

Ranch Hand

Posts: 71

posted 13 years ago

I'm guessing e is a String? If its a number, then Nimo has already shown you how to test for any positive number. If however this is a String and you are looking for only positive numeric characters, then look at the JavaDocs on java.util.regex. You'd need a very simple pattern ('\d' or '\p' if its a floating point number)

posted 13 years ago

If e is a string, you could also convert it into a numeric type, such as int or double, and then use e > o, as shown earlier. For example, to convert a string containing a numeric value into an int, you can use Integer.parseInt(). To convert a numeric string into an double, use Double.parseDouble().

For my latest books on Java, including *Introducing JavaFX 8 Programming*, see HerbSchildt.com

Peter Shipway

Ranch Hand

Posts: 71

posted 13 years ago

Ive tried and ive tired and i just cant get my head around this question (its in a text book im doing);

A user inputs a certain amount of ram, the computer then outputs how many of 64, 128, 512 and 1024 memory chips are required, it should work in the best value way, e.g 256 = 2 128 chips, not 4 64 chips.

I know that somehow i have to / by each memory amount to get the answer, but ive tried using wild cards to check which is greater than or less then but its not working , any hints, code examples (or the whole code << yeah right lol) would be greatly aperciated, thank you.

A user inputs a certain amount of ram, the computer then outputs how many of 64, 128, 512 and 1024 memory chips are required, it should work in the best value way, e.g 256 = 2 128 chips, not 4 64 chips.

I know that somehow i have to / by each memory amount to get the answer, but ive tried using wild cards to check which is greater than or less then but its not working , any hints, code examples (or the whole code << yeah right lol) would be greatly aperciated, thank you.

posted 13 years ago

You don't *have* to use division, though using it would be more efficient.

You could use repeated subtraction by 1024, checking that the result is positive each time, and counting the number of times you have to make a subtraction. When the amount is less than 1024, move to the next largest amount.

From reading your post I'm not sure exactly where your problem is....can you read the user input OK?

If you post your errors / code here people will be able to help you more easily.

Cheers,

--Tim

You could use repeated subtraction by 1024, checking that the result is positive each time, and counting the number of times you have to make a subtraction. When the amount is less than 1024, move to the next largest amount.

From reading your post I'm not sure exactly where your problem is....can you read the user input OK?

If you post your errors / code here people will be able to help you more easily.

Cheers,

--Tim

posted 13 years ago

I think it was Polya who said in his book "How to solve it" that one method for approaching a difficult task is to consider a simpler case.

So, try thinking about how you would solve it if you ONLY had 1024MB memory chips.

if i input 7168, how would you figure out how many chips are required? what if i put in 14,336?

once you solve that, add one more size chip, like the 512. then try and solve some made up cases, like 1536 or 7680... then add a third size... eventually you should see a general solution.

OR, try it will even smaller numbers... what if you only had memory chips that were 1, 2, 4, and 8 ? how would you solve it for a value of 7? for 39?

So, try thinking about how you would solve it if you ONLY had 1024MB memory chips.

if i input 7168, how would you figure out how many chips are required? what if i put in 14,336?

once you solve that, add one more size chip, like the 512. then try and solve some made up cases, like 1536 or 7680... then add a third size... eventually you should see a general solution.

OR, try it will even smaller numbers... what if you only had memory chips that were 1, 2, 4, and 8 ? how would you solve it for a value of 7? for 39?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors