Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

help

Thread view: 
ricky - 07 Dec 2006 09:23 GMT
I am trying to write a function to check if number is greater than 10.
if it is. somehow parse the two digit number into first and second
digit and add the two together and get the result

for ex. num =23

if(num<10)
System.out.println.(num)
else
(get first digit) and  (get second digit)
//add the two number now
int newnumber = firstdigit(2)+seconddigit(3)

So over here num i have(23) which is greater than 10. So it's first
digit(2) and second digit(3), should be added together and answer i'll
get is 5, which will be my final answer

I don't know if i have explained it properly. I am very new to java
programming and writing a code for my final year project. I am stuck on
this bit.

If someone can help me . I'll be greatful

Thanks in advance.
ricky - 07 Dec 2006 09:31 GMT
basically the only thing i been able to think so far is.
if(num<10)
System.out.println.(num)
else
num/10
//that will give me quotient and remainder
// and  i'll add the two togther
int newnumber = quotient + remainder

but again i don't know how to get quotient and remainder

> I am trying to write a function to check if number is greater than 10.
> if it is. somehow parse the two digit number into first and second
[quoted text clipped - 20 lines]
>
> Thanks in advance.
sheismylife@gmail.com - 07 Dec 2006 09:46 GMT
23/10= 2
23-10*2=3

>  basically the only thing i been able to think so far is.
> if(num<10)
[quoted text clipped - 31 lines]
>
> > Thanks in advance.- 隐藏被引用文字 -- 显示引用的文字 -
Lew - 07 Dec 2006 18:28 GMT
> 23/10= 2
> 23-10*2=3

23 % 10 == 3

To the OP:

These are fundamental language elements. By the time you get to a "final year
project" you really should already know about the integer division and
remainder operators.

Check out the Sun Java tutorials and also Bruce Eckel's book, _Thinking in
Java_, which is available in a free online edition.

> System.out.println.(num)

This statement will not compile.

Try doing things the "Shanahan way": write a skeleton of the overall Java
program that compiles correctly. You can even go so far as to just put in the
raw class definition and nothing else:

package finalproject;
public class DigitAdder
{
}

as a first step. Once that compiles correctly, expand it to:

package finalproject;
public class DigitAdder
{
    public int digitAdd( int num )
    {
      return 0;

    }
}

Once that works, then add a main() method that instantiates a DigitAdder and
invokes the object's digitAdd() method.

Then revise digitAdd() to return correct results.

Continue onward from there.

- Lew
Alex Hunsley - 07 Dec 2006 17:24 GMT
>  basically the only thing i been able to think so far is.
> if(num<10)
[quoted text clipped - 6 lines]
>
> but again i don't know how to get quotient and remainder

Please don't top-post!

Have a look at the modulo operator, which is written %, and can be used
to calculate remainders:

 int num = 23;
 int remainder = num % 10;
 int leftDigit = (num - remainder) / 2;
 int newNumber = leftDigit + remainder;

There are other ways of doing it, but this quite a succinct way of doing
what you want.
lex
Simon Brooke - 07 Dec 2006 18:05 GMT
> I am trying to write a function to check if number is greater than 10.
> if it is. somehow parse the two digit number into first and second
[quoted text clipped - 8 lines]
> //add the two number now
> int newnumber = firstdigit(2)+seconddigit(3)

       if ( n > 10)
       {
               n = (int)(n/10) + n % 10;
       }
       System.out.println( new Integer(n).toString());

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

       Tony Blair's epitaph, #1:       Tony Blair lies here.
       Tony Blair's epitaph, #2:       Trust me.

Lew - 07 Dec 2006 19:07 GMT
> if ( n > 10)
> {
>         n = (int)(n/10) + n % 10;
> }
> System.out.println( new Integer(n).toString());

Incidentally, this will work for all n, positive, negative or zero.

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.