Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Leading zeros  RSS feed

 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi am trying to accomplish this. When the first int in the file Multiply.txt i.e. 13 and 17 as shown below is greater than the second integer 8 and 16 i multiply it by 2. My issue is that i get leading zeros for my results.
I need your help please

Input file
13,8
17,16

output problem
0
16
0
32

Expected output
64
32

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Molayo Decker wrote:Hi am trying to accomplish this. When the first int in the file Multiply.txt i.e. 13 and 17 as shown below is greater than the second integer 8 and 16 i multiply it by 2.

Multiply what by 2? I'm afraid your explanation doesn't make sense based on your "expected results". In fact, the ones you get AFTER your 0 are much closer to what I would expect.

Question: What does that loop at line 11 do, and how many times will it run?

Also: A minor point, but if you're not using 'line' outside the loop, you can code it as:
  for (String line = buffer.readLine(); line != null; line = buffer.readLine()) { ...

HIH

Winston
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston i have a file that contains 13,8 in that other. If 8 is less than 13 i multiply 8 by 2 i.e. 8 X 2 to get 64. Line 11 loops through the array and multiple 8 x 2 till 8 is equal or greater than 13.

Am trying to get rid of the leading zeros. Am i making sense?
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Molayo Decker wrote:
Am trying to get rid of the leading zeros. Am i making sense?


Hi, Molayo. If you are wondering why you are seeing those zeroes, try thinking about Winston's question...

What does the loop at line 11 do, and how many times does it run?


More broadly, I too am having a hard time understanding the desired behavior of your program -- I wonder if it is completely clear in your mind?

If you have the pair x,y...

If x>y what should happen next?
If x<y what should happen next?
If x=y what should happen next?

Is your calculation supposed to be done in a single step, or are there multiple steps involved?

Kind regards,

Scott
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code is very simply but i guess my explanation is still not clear. If you take a look the Multiply method from line 22 to 24 am just checking to see if the integer in array index One is smaller or equal to Array index Zero. If that is the case the it multiplies array index 1 by 2.

It is just during a single action so far as array index 1 which is 8 is less than array index 0 which is 13.

so give x and y if y < x ... do y * 2 until y is equal or greater than x.

Am not during any addition or complex algorithm. Am just multiplying 8 * 2 still it is greater than 13

I just want to get rid of the leading zeros
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway thanks i resolved it

 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have tried to read all this thread few times. I'm not sure you got all parts right.

Molayo Decker wrote:If 8 is less than 13 i multiply 8 by 2 i.e. 8 X 2 to get 64. Line 11 loops through the array and multiple 8 x 2 till 8 is equal or greater than 13.
Something is wrong here. 8 x 2 is not 64, only 16, 8^2 is 64. Anyway, that loop on line 11 I don't think it does what you saying.

I think you need to start over, but this time with a pencil and a piece of paper. On a positive side, Winston wrote earlier a very good tutorial (<- check this out), which should help you to change the way you think about programming. At the moment it appears you haven't got clear understanding what needs to be done. From the quite a lot code you have written we see that you know Java syntax, that is good, but probably to early you've got your hands dirty with writing it.

Please try to write down the steps in english, what are you trying to accomplish by not going into deep details. For a second round, write down again, but this time by providing more details. And do it again and again once you are clear about the steps you need to do.

From those steps later on you should be able to identify and decompose your task into small subtasks, where each of them should be a method, which could be implemented independently from each other and tested for the correctness.

As you see, this fairly easy task (well, we don't know that yet, might it is more complicated than I think, as I still have some unanswered questions) caused you a headache, because of the lack of planning.

[edit] Improved grammar. Sorry about that.
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, I should spend more time putting thoughts together and writing pseudocode. It makes things easier. I know what i did in a bad habit, but guess it's just laziness.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!