Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-27-2004, 12:58 PM   #1 (permalink)
freesoft_2000
Code Monkey
 
Join Date: Oct 2004
Posts: 51
freesoft_2000 is on a distinguished road
Question Odd And Even

Hi everyone,

I am doing a simple program with some text in the jtextpane and the text inside the jtextpane span to about three standard A4 papers. What i want to do is to only be able print even and odd pages. I am using currently the java printable interface in which i must overide the print method which has the following declaration

public int print(Graphics pg, PageFormat pageFormat, int pageIndex)
{
}

I Know that printing even or odd pages does not involve the graphics or the pageformat objects.

The pageindex object is the one i am concerned about.

The thing is i do not know how to test as to whether the pageindex is an even or odd number (including 0 which is an odd number)

Can anyone show me any codings or explain to me in detail how to test for even or odd numbers.

Thank You

Yours Sincerely

Richard West
freesoft_2000 is offline   Reply With Quote
Old 10-27-2004, 01:17 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
That's one of those things in Java I never found out if there was a good way of doing it. It's kinda odd that Integer or Math doesn't have an even/odd method. But even so, this is pretty a basic computer science/math problem, so there are probably a million different ways to do it.

I'm assuming we're working with integers here. The result of any mathematical operation that has a fraction/decimal will have the partial number chopped off, not rounded (good thing in this case). As a result, you can do something like this:

Code:
int a = 15;
int b = a \ 2; // Might want to idiot check me on the divide symbol.
if ( (b * 2) == a ){
  // It's even
}else{
  // It's odd
}
The result of this is that b will equal 7, because the .5 gets chopped off. Because 7 * 2 != 15, we know it's odd.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-27-2004, 01:22 PM   #3 (permalink)
inkedmn
Registered User
 
Join Date: Mar 2004
Location: Fullerton, CA
Posts: 26
inkedmn is on a distinguished road
Send a message via AIM to inkedmn
the easiest way would be something like:

Code:
if(pageindex % 2 == 0)
    // then it's even
else
    // then it's odd
the modulus operator (%) returns the remainder of any division operation. Here's a python example (since it's easier to read/understand)

Code:
>>> 3 % 2
1
>>> 14 % 7
0
because 14 divides evenly by 7, there's no remainder, but 3 doesn't divide evenly by 2, so there's a remainder of 1, etc.

hth
inkedmn is offline   Reply With Quote
Old 10-27-2004, 01:27 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Ah yeah, I forgot about modulus. That's an even better way to do it.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:08 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting