# Thread: how should i begin..

1. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0

## how should i begin..

Trying to design an application that determines and prints the number of odd,even,and zero digits in an interger value read from keyboard.Guys i've done problems similar to this one and always get stuck in the same spot..
Java Code:
```import java.util.Scanner;
public class smoked {

public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter any Interger:");
scan.nextInt();
int j = scan.nextInt();

}
}:)```

2. Member
Join Date
Sep 2010
Location
Oregon, usa
Posts
69
Rep Power
0
You have your integer entered from the key board stored in variable j.
All you need to do is code the "math" on that integer and do a System.out.print() on your calculated results.

example: if you wanted to add 5 to your integer and print the result:
int result = j + 5;
System.out.print(result);

If it's the "math" part that you are having trouble with, think about how you would solve the problem (or prove it mathematically) by hand and then try to code it from there.

example: how to determine if a number is even or odd:
divide by two and see if the result is a whole number, if the result is a whole number then the remainder would be 0, thus your number is even -- you could then look at the Math class Math (Java Platform SE 6) and Arithmetic Operators Summary of Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) to see what will help you solve the problem.

I hope this helps. :)
Last edited by tashimoto; 10-21-2010 at 01:10 AM.

3. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
Hey thanks for your response .. so i've got it down with the checking if the interger is odd/even. Now to check and analyze every individual number in the interger , could i do something like a while loop, that keeps on taking the remainder of the interger to seperate each number then analyze each number ??
Java Code:
```import java.util.Scanner;
public class smoked {

public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter any Interger:");
int holder = scan.nextInt();
if (holder % 2 == 0 ) {
System.out.println("I am an even number");
} else {
System.out.println("I am an odd number");

}
}
}```

4. Member
Join Date
Sep 2010
Location
Oregon, usa
Posts
69
Rep Power
0
Originally Posted by Bgreen7887
Now to check and analyze every individual number in the interger
Do you mean, if the integer entered by the user is 345, then the program checks the 3, the 4, and the 5 (as in 300, 40, 5) or is it supposed to check every number below 345 : 344, 343, 342, 341... back down to 0 ??

The first way is breaking down the number into it's 100s, 10s, 1s place to find each individual number. The other way is decrementing the last number by 1.

Both would need a loop of some sort. Try breaking it down by hand solving smaller integers to find the mathematical "patterns" ... any repeating patterns usually require a loop.

Hope this helps! :)

5. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
HEY IT TOOK ME A WHILE BUT I GOT IT THANKS TOO ALL YOUR HELP, HERE IT IS
Java Code:
```import java.util.Scanner;
public class smoked {

public static void main(String[]args)
{
int x = 0;
int y,holder,holder2;
Scanner scan = new Scanner(System.in);
System.out.print("Enter any Interger:");
holder = scan.nextInt();
while (holder > 0)
{
holder2 = holder % 10;
holder = holder  /10;
System.out.println(holder2);
if ( holder2 % 2 != 0 ) {
System.out.println("Im an odd Interger");
} else {
System.out.println("im an even Interger");
}
x++;

}
}
}// GETTING STARTED IS MY BIGGEST HURDLE```

6. Member
Join Date
Sep 2010
Location
Oregon, usa
Posts
69
Rep Power
0
Cool!

For other problems similar to this one, it also might help to list out your variables and what value they contain as you design your loops, especially when using arrays. It might help visualize what is happening in the loops. example: var[i] in a loop, write the value of var[0] and i, then var[1] and i, then var[2] and i ... and so on. It really helps to do this with nested loops! :)

Also, some other things to think about is code format... Class names should begin with a capital letter while variables begin with lower case letters: "smoked" should be "Smoked". In larger projects, it makes the code easier for others to read.

Also, when I was starting out, a handy piece of code to add to all your projects is a while loop that allows the user to continue or quit. Then you can run your code with multiple entries.

Good luck with your other projects! :)

7. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
Thanks alot i'll probably have a ton more questions..Im hooked and trying to get better faster.
Class is getting quite intense and i cant be left behind, im doing all the projects from the book!

#### Posting Permissions

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