Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java int variable

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a integer variable in main method.
lets say int num=3487;

I want to know how can I print each and every digit of it separately.
Like 1 st digit will be 3 from left then second is 4 from left and so on..

Appreciate your suggestions on this..
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show in an example what you mean?

I'm confused with the "2nd from the left" piece.

Also, since int is a primitive, you may want to convert it to a String.

WP
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way is probably to convert it to a String and then use the getChar method to get each character of the String
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Common beginner’s exercise, which you are supposed to do with the % and / operators. It is usually easier to print the last digit first.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear..Sure will keep in mind
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so now somehow I have written the code to find the digits separately.Below is the code.

BufferedReader k=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
try {
int num=Integer.parseInt(k.readLine());
int n=num;

System.out.println("number you entered is" +n);
String string=Integer.toString(n);
int[] digits=new int[string.length()];
System.out.println(digits);
for(int i=0;i<string.length();i++){
digits[i]= Integer.parseInt(string.substring(i,i+1));

}
System.out.println("thedigits are" +Arrays.toString(digits));

Now my intention is make the programme run like below..

I have an integer number lets say 4567.
The programme output will print 410614(every second digit will be added with it self)
example->
int num=4567.
second digit is four(4) here,so in output it will add 5+5=10
then fourth digit is seven(7) here,so in output it will be 7+7=14.
Finally it will give me the whole output as 410614.

got struck here.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have an integer number lets say 4567.
The programme output will print 410614(every second digit will be added with it self)
example->
int num=4567.
second digit is five(5) here,so in output it will add 5+5=10
then fourth digit is seven(7) here,so in output it will be 7+7=14.
Finally it will give me the whole output as 410614.

got struck here.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@William...
Is that example is good to go.Or you want more clarification.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, so from your example of 4567 ...

I am using [] to highlight the processing.

[4][5][6][7] = [4][10][6][14] ?

so as you iterate over the source, you need to determine if the current index is divisible by 2, but arrays start at 0, so there's a clue. ;)

WP
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
expansion of Campbell suggestion:
while(true)
int remainder = num % 10
print *remainder*
num = num / 10; // get remaning digits
if num == 0 then break;
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:while(true)


um, what??
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic