• Post Reply Bookmark Topic Watch Topic
  • New Topic

fun with integers!  RSS feed

 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A friend of mine is taking C in school and he sends me over programming problems he gets so I can give it a shot in Java. I am not sure how to do this one?




Thanks!
d.jpg
[Thumbnail for d.jpg]
Assignment
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would probably be easiest to do this by turning the input integer into a String. Using a for loop and the substring method from the String class, you should be able to achieve this.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:It would probably be easiest to do this by turning the input integer into a String. Using a for loop and the substring method from the String class, you should be able to achieve this.



Thanks for the response ill take these tips and give it a go!

 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how it turned out Any idea how this could be done mathematically using just ints?


 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.Scanner
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partial Data wrote:Any idea how this could be done mathematically using just ints?


An idea: without error handling. The doubles behind the computation are not spelled out.

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan

What about values such as 101 and 1010?
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:


How do i implement this? I know i need to call the method from Main, and pass in the integer from the user input. I am not getting how to loop this and then display it.

Thanks for the help!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partial Data wrote: I know i need to call the method from Main, and pass in the integer from the user input. I am not getting how to loop this and then display it.

Do it one piece at a time:

1) write the code that calls the method. compile. perhaps write code to prove that the method got called (hint: System.out.println("I'm in the method");"

2) once you KNOW the above works, update it to "pass in the integer from the user input". Write some code to prove you passed in the user input (hint: System.out.println("value passed in: " + x);"

3) Once you KNOW the above works, write the code that displays what this method returns.

4) Once you KNOW you can do it once, you can simply write the loop that wraps the method call.

Whatever you do, do NOT try and do this all in one go. The less code you write before you recompile and test, the easier it is.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Partial Data wrote: I know i need to call the method from Main, and pass in the integer from the user input. I am not getting how to loop this and then display it.

Do it one piece at a time:

1) write the code that calls the method. compile. perhaps write code to prove that the method got called (hint: System.out.println("I'm in the method");"

2) once you KNOW the above works, update it to "pass in the integer from the user input". Write some code to prove you passed in the user input (hint: System.out.println("value passed in: " + x);"

3) Once you KNOW the above works, write the code that displays what this method returns.

4) Once you KNOW you can do it once, you can simply write the loop that wraps the method call.

Whatever you do, do NOT try and do this all in one go. The less code you write before you recompile and test, the easier it is.





I get a bit a head of myself Thanks for helping me calm down. Below is what im having trouble with. I am not sure how to return the value back or how the loop should be constructed.

here is what i have. I am able to see the it work if i set a break point at return results; but that never gets passed to Main.




 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:
What about values such as 101 and 1010?


How do you want to distnigush between 01 and 1 on an interegr-only basis?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan

My point is your solution does not work for those input values.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:

My point is your solution does not work for those input values.



Well, how are you supposed to distinguish between 01 and 1 on an integer basis?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan

The OP asked if this problem could be achieved purely using integers. You posted a potential solution using just integers. I questioned whether it would work for numeric values which, when stripped of the first digit, would contain one or more leading 0s.

So, for input value "101", the output should be:

101
01
1

as opposed to:

101
1

It is looking like a integer-only solution is not feasible here.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Ivan

The OP asked if this problem could be achieved purely using integers. You posted a potential solution using just integers. I questioned whether it would work for numeric values which, when stripped of the first digit, would contain one or more leading 0s.

So, for input value "101", the output should be:

101
01
1

as opposed to:

101
1

It is looking like a integer-only solution is not feasible here.


I guess you could limit input to numbers containing only non-zero digits.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel

But that doesn't meet the stated requirements. The only numbers to be ignored are those which are negative.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so here is what i am running into



the out put:

Enter a number:
123456
6
56
456
3456


So any idea how I could tackle this?

Thank you!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partial Data wrote:So any idea how I could tackle this?

Well, this is one rare case where I think you might be better off using a String rather than a number:
1. Convert the number to a String.
2. For each 'digit' in it, display the correct number of spaces (which might be 0), and the substring starting at the relevant 'digit'.

Winston
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with System.out.printf(%9d%n", myNumber);?
And I like the technique shown earlier with the % operator and 10, 100, 1000 etc.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is wrong with[tt] System.out.printf(%9d%n", myNumber);

Well for one, it doesn't compile
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is wrong with System.out.printf(%9d%n", myNumber);?
And I like the technique shown earlier with the % operator and 10, 100, 1000 etc.


I am not understanding how to implement that by looping it in the method and pass the return back from the method showing the results.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partial Data wrote:I am not understanding how to implement that by looping it in the method and pass the return back from the method showing the results.

Then I suggest you read the answers you've been given, or tell us exactly what it is that you don't understand about the process. ItDoesntWorkIsUseless (←click).

Winston
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Partial Data wrote:I am not understanding how to implement that by looping it in the method and pass the return back from the method showing the results.

Then I suggest you read the answers you've been given, or tell us exactly what it is that you don't understand about the process. ItDoesntWorkIsUseless (←click).

Winston



I have scroll up I never once gave a "it dont work" kind of answer. I have provided code, tried to implemented what has been given and I have explained that I do not understand:

what loop I should use in the method?
how to return a result from the method?

I appreciate your help.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I" can't" see" anything" the compiler" wouldn't" like."
You wrote a loop with the % operator yourself. All you have to do is use the printf call which Joel Christophel thinks the compiler wouldn't like instead of println. You can read about printf here under formatting.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I" can't" see" anything" the compiler" wouldn't" like."
You wrote a loop with the % operator yourself. All you have to do is use the printf call which Joel Christophel thinks the compiler wouldn't like instead of println. You can read about printf here under formatting.



Ok will do thank you.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the %d will have problems if you have a 0 in the middle of the number, as somebody said earlier. The suggestion about using a String would sort out that problem.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Split the number into an int[] whose elements are the individual digits? You would need both the divide and remainder operators for that.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dade Murphy wrote:I have scroll up

Really? Then you clearly missed both my and Campbell's posts.

I have provided code, tried to implemented what has been given and I have explained that I do not understand:
what loop I should use in the method?
how to return a result from the method?

I hate to say, but if you don't know how to return a result from a method then maybe you're not ready for a problem like this without further reading of the tutorials, or a couple more weeks of classes.

Both Campbell and I have given you specific ideas on how to do this differently (and, to be honest, I think his solution is better than mine because it doesn't involve ANY looping at all); but if you're bound and determined to use only the code you've written, then you'd better read up on:
(a) loops.
(b) returning values from methods.

Winston
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Dade Murphy wrote:I have scroll up

Really? Then you clearly missed both my and Campbell's posts.

I have provided code, tried to implemented what has been given and I have explained that I do not understand:
what loop I should use in the method?
how to return a result from the method?

I hate to say, but if you don't know how to return a result from a method then maybe you're not ready for a problem like this without further reading of the tutorials, or a couple more weeks of classes.

Both Campbell and I have given you specific ideas on how to do this differently (and, to be honest, I think his solution is better than mine because it doesn't involve ANY looping at all); but if you're bound and determined to use only the code you've written, then you'd better read up on:
(a) loops.
(b) returning values from methods.

Winston


I shall. thanks
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dade Murphy wrote: . . . thanks
You're welcome
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally figured it out thanks for the guidance. I hope this can help others



 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!