Help finding positions of an integer (substring but for int)

I need help returning a number from an integer by its position

Page 1 of 1

11 Replies - 7570 Views - Last Post: 28 January 2010 - 02:35 PM Rate Topic: -----

#1 Hawkeye28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Help finding positions of an integer (substring but for int)

Post icon  Posted 27 January 2010 - 10:40 PM

I have a 4 digit integer. I need to output each digit, one at a time from the integer. Example: if it were 1234
I would need to return each digit on a seperate line. My problem is this, I have to keep it an integer. I wrote the program converting it to a string and using the substring method, pretty simple. I need to use some math now to do this and I am stuck. Example: 1234 would be newNum = num / 1000 gives me the first digit, after that I am stuck. I need some assistance. Below is the part of my code as it is using a string to better explain what I want to do. I don't expect you to do this for me, but some advice on how to approach it would be nice.
Thanks in advance.



			if (num.length == 4)
			{
				System.out.print(num.Substring(0, 1));
				System.out.print(num.Substring(1, 1));
				System.out.print(num.Substring(2, 1));
				System.out.print(num.Substring(3, 1));
			 }




Is This A Good Question/Topic? 0
  • +

Replies To: Help finding positions of an integer (substring but for int)

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Help finding positions of an integer (substring but for int)

Posted 27 January 2010 - 11:10 PM

Brute force:
int number;
int ten3;
int ten2;
int ten1;
int ten0;

number = 1234;

ten3 = number / 1000;
ten2 = (number - (ten3 * 1000)) / 100;
ten1 = (number - ((ten3 * 1000) + (ten2 * 100))) / 10;
ten0 = number - (((ten3 * 1000) + (ten2 * 100)) + ten1 * 10);

System.out.println(ten3 + " " +  ten2 + " " + ten1 + " " + ten0);



:)
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,691
  • Joined: 27-December 08

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 08:09 AM

Better yet- use a for loop and take advantage of some automation.

public void printNum(int n){
	while(n > 0){ //integer division truncates anything after the decimal, so x/10, where x < 10, == 0
		 //prints out the end digit
		 System.out.println(n%10);

		 n /= 10; //divide n by 10, so 1234 becomes 123 
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 Hawkeye28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 09:27 AM

Thanks both of you! When using the last post (macosxnerd101) it is very efficient, however it prints the digits backwards. IE 2009 prints 9002. I need it to reverse and print 2009. I have modified the code to no avail. How can I reverse the digits. Thanks!

This post has been edited by Hawkeye28: 28 January 2010 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 10:13 AM

Hawkeye28, on 28 Jan, 2010 - 08:27 AM, said:

How can I reverse the digits. Thanks!


Just convert the integer into char array and then swap the places for example first number will swap place with the last one and so on...

all the best :^:

Ps:i am also a beginner in java. :)

take care

This post has been edited by vyom.dev: 28 January 2010 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 Hawkeye28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 10:40 AM

The number needs to remain an integer as part of the assignment. the code is good, I just need to reverse the output.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,691
  • Joined: 27-December 08

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 10:40 AM

There's actually a more memory efficient way. If you know that your number is going to be 4 digits, set a temp variable to 1000 b/c a 4 digit number/1000 will give you the first digit. Then, assign x the remainder of itself/y and divide y by 10. Like so:

int x = 1234;
int y = 1000;
while(x > 0){
	   System.out.println(x/y);
	   x %= y;
	   y /= 10;
}


Was This Post Helpful? 1
  • +
  • -

#8 Hawkeye28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 10:56 AM

Actually it should be 4 digit, but it wont be validated and should still function the same way if it is longer or shorter. The code works fine for any length of integer, just backwords.

This post has been edited by Hawkeye28: 28 January 2010 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 Hawkeye28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 11:32 AM

This is working for me. It pads left 0's if it is less than 4 digits, but I thinks this works out better given it is supposed to input 4 digits and output 4 digits in the first place. Anything shorter would be an error. In this case it is not nessassry to validate the input
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,691
  • Joined: 27-December 08

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 12:43 PM

Really, the best policy would be to use Strings and the charAt() method, especially since they have a length() method. Numbers, whether you use the primitives or their wrapper classes, don't have a length() method or a length attribute like arrays do. Because of this, it is easier to automate this process with Strings than with numeric primitives.
Was This Post Helpful? 0
  • +
  • -

#11 Hawkeye28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 02:27 PM

Yeah, that is the way I had it first off. My instructor is big on Math even if it is easier to keep it a string and use the Length function. Which took about 15 minutes to write the porgram. thanks for your help though!
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Help finding positions of an integer (substring but for int)

Posted 28 January 2010 - 02:35 PM

Since macosxnerd101's is always in reverse, we can still use it, we just have to print backwards to forwards instead of the other way around. You can do this by recursively printing each character. To do this, recurse through dividing by 10 until you hit 0 (end of the int), then as you return back to the beginning, print each character.

public static void printNum(int n){
		if (n <= 0) { // If the answer == 0, then we're at the end
			return;
		}

		printNum(n/10); // Not at the end yet so keep going

		// Now that we're at the end of the string, each method call is going
		// to print the current character.
		System.out.println(n%10); 
	}



If you haven't run into recursion yet, this is a little tricky...but see if you can figure it out and if you can't tell me, but for now, let me demonstrate what is happening.

Say we send 20099 through...
printNum(20099); --> calls this with 1 less number
	printNum(2009); --> etc
		printNum(200);
			printNum(20);
				printNum(2);
					printNum(0) --> Return
					 .. Now we on the System.out.println method call
				System.out.println(2);
			System.out.println(0);
		System.out.println(0);
	System.out.println(9);
System.out.println(9);



Again, good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1