Results 1 to 14 of 14
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default How to Print every tenth integer in my output

    I have created a program that finds all five-digit palindromes (A palindrome is a number which has the same digit from either end such as 12921). Right now I have all of the palindromes printing to the output. Since the output is so large, I want to only print every tenth palindrome but cannot figure out how to do it.
    Here's what I have.

    Java Code:
    public class ProgrammingProblemOne {
    
        public static void main(String[] args){
    
        	for(int i = 10000; i <= 100000; i++){
    
        		//create variable first digit in order to isolate the first digit
        		int firstDigit = i / 10000;
    
        		//Create variables a and secondDigit in order to isolate the second digit
        		int a = i / 1000;
        		int secondDigit = a % 10;
    
        		//Create variables b and thirdDigit in order to isolate the third digit
        		int b = i / 100;
        		int thirdDigit = b % 10;
    
        		//Create variables c and fourthDigit in order to isolate the fourth digit
        		int c = i / 10;
        		int fourthDigit = c % 10;
    
        		////Create variable fifthDigit in order to isolate the fifth digit
        		int fifthDigit = i % 10;
    
        			//check to see if number is a palindrome
        			if(firstDigit == fifthDigit && secondDigit == fourthDigit)
        				System.out.println("the five digit palidrome numbers are: " + i);
        	}
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Use the % operator on the index i.

    [Edit] Whoops! Maintain a second counter that you increment every time you get a palendrome. And use % with that counter to determine whether you print or not.

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Use the % operator on the index i.

    [Edit] Whoops! Maintain a second counter that you increment every time you get a palendrome. And use % with that counter to determine whether you print or not.
    I had a feeling that % would be involved somehow. I tried something like this but I didn't know what I was doing. Here's what I tried (don't laugh too hard)

    Java Code:
    if(firstDigit == fifthDigit && secondDigit == fourthDigit)
        				//count the palidrones
        				palidrone++;
        				int output = i % 10;
    
        				System.out.println("the five digit palidrome numbers are: " + output);
    This runs infinitely and only prints one digit. I also tried int output = palidrone % 10; but that didn't work either.

    I'll try adding a palindrome variable and counter to my for loop and see if that works.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Count the palindromes as you are doing, but use a if statement. Something like

    Java Code:
    if(++palindrome % 10 == 0) {
        System.out.println(i);
    }
    The condition in the if statement both increments the palindrome counter and checks it.

  5. #5
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Have you thought of adding all your palindromes to an arrayList as they are running, and after you've collected them, have something like this :)

    Java Code:
    for (int z = 0; z <myList.size(); z+=10){
    System.out.println(myList.get(0));
    }
    You can read on arrayLists here:
    ArrayList (Java 2 Platform SE v1.4.2)

    Hint, your arraylist will be ArrayList<Integer> type :D

  6. #6
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Almost forgot! If you like palindromes so much, you might want to read on this very useful datastructure, which will allow you to check an arbitrary number for palindromes very easily :D
    Stack (Java 2 Platform SE 5.0)

  7. #7
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Count the palindromes as you are doing, but use a if statement. Something like

    Java Code:
    if(++palindrome % 10 == 0) {
        System.out.println(i);
    }
    The condition in the if statement both increments the palindrome counter and checks it.
    Cool, the first time I've been able to use a nested if statement. Something I noticed when using if statements inside of loops is that you cannot use the braces you would normally use with if statements.

    Thank you very much for your help.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

    With respect to braces: it is a good idea to always use braces. Every time. Even when the block of code has only a single line. Otherwise you *will* get tripped up once the statements nest and, more so, when there are "else" bits involved.

  9. #9
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    With respect to braces: it is a good idea to always use braces.
    Nah, 6-12 months is enough for most people. Then you'll have beautiful teeth.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I meant to ask: is there some reason why you are looking at every int over the range and making the computer "pick them apart"?

    An alternative would be to just generate the numbers with palendromic decimal representation. They all look like xyzyx where x, y, z vary over a suitable range. You could nested for loops to do this. If my counting isn't awry there will be 9*9*9 such numbers.

    [Edit] it was. 9*10*10. Or something.
    Last edited by pbrockway2; 04-16-2011 at 02:55 AM.

  11. #11
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You're welcome.

    With respect to braces: it is a good idea to always use braces. Every time. Even when the block of code has only a single line. Otherwise you *will* get tripped up once the statements nest and, more so, when there are "else" bits involved.
    That's what I thought but when I tried to use the braces that normally go along with the if statement within the for loop I get all kinds of compile problems. Once I get rid of the if statement braces and only keep the for loop braces the program works.

    I retried the braces and I was able to get the program to work. I must have screwed them up somehow before
    Last edited by jim01; 04-16-2011 at 03:01 AM. Reason: Whoops!

  12. #12
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Maximus-EVG View Post
    Have you thought of adding all your palindromes to an arrayList as they are running, and after you've collected them, have something like this :)

    Java Code:
    for (int z = 0; z <myList.size(); z+=10){
    System.out.println(myList.get(0));
    }
    You can read on arrayLists here:
    ArrayList (Java 2 Platform SE v1.4.2)

    Hint, your arraylist will be ArrayList<Integer> type :D
    I've been hearing a lot about arrays lately but we don't get to that chapter until Monday. They sounds like they will make life much easier. Right now it's just loops though.

  13. #13
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by ojn View Post
    Nah, 6-12 months is enough for most people. Then you'll have beautiful teeth.
    LOL! Nice.

  14. #14
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    I meant to ask: is there some reason why you are looking at every int over the range and making the computer "pick them apart"?

    An alternative would be to just generate the numbers with palendromic decimal representation. They all look like xyzyx where x, y, z vary over a suitable range. You could nested for loops to do this. If my counting isn't awry there will be 9*9*9 such numbers.

    [Edit] it was. 9*10*10. Or something.
    Last week we did branching and one of the programs was to write a program that inputs a 5-digit integer from the keyboard and prints whether or not the
    input number is a palindrome or not. It seemed like the easiest way to write this weeks program was to tweak last weeks.

    I didn't realize that nested for loops would do the same job. I guess I need to go back and see how that works.

Similar Threads

  1. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  2. Print Variable From One Class to Output on another.
    By Lyricid in forum New To Java
    Replies: 8
    Last Post: 02-27-2010, 01:56 PM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  5. how to print output on same line in 'while loop'?
    By acidblue in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •