# Thread: I'm new to JAVA, stuck on "int"...Need some help getting this stuff straight, thanks!

1. Member Join Date
Sep 2009
Posts
4
Rep Power
0

## I'm new to JAVA, stuck on "int"...Need some help getting this stuff straight, thanks!

Asks the user to enter a 5 digit positive integer
Calculate each digit using integer operations / and %.
Calculate and display the sum of the digits.

Heres what I got so far:

import java.util.Scanner;

/* Mike
* Testing Digits Program
* This is my work
*/

public class Digits
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a 5 digit number:");
String digits = keyboard.nextLine();

int fi,se,th,fr,ft;
fi = (x/10000);
se = (x%1000)/1000;
th = (x%100)/100;
fr =
ft =
System.out.println()

}
}

I've got this far then i get errors left and right...and I'm looking for a similar question like it in the book and can't seem to find one. And this % is new to me I think I'm using it wrong so i left fr (fourth) and ft (fifth) . For example if i place 12345 i want the program to sum 1+2+3+4+5 which equals displaying 15...Thanks,, any contribution appreciated thanks.  Reply With Quote

2. ## Hi,
1.Just take a paper and write what u want to do.
2.Implement the above one as a code by putting the comments properly
3.Try to compile.
4.Then ask us doubts where u got stuck up by bolding ur problematic line.

-Regards
Ramya  Reply With Quote

3. Member Join Date
Sep 2009
Posts
4
Rep Power
0

## I'm just getting started with an intro class to programing
1)I believe I am going to need to make a scanner so input can be inputted
2)Here is where I get stuck, in other scenerios like one found in the book...An age question can be asked
System. out. print ( "What is your age? ");
age = keyboard. nextInt( ) ;
But the question is asking me to difine different digits. I figured out that each of the digits needs to be looked at differently and uniquely, so I short named each of the digits as above, and stuck here again. How am I going to connect the int names to the display sum

I believe the last part should show System.out.println(fi+se+th+fr+ft)

I'm reading through the book now, the book isn't explaining as well. Thanks for your patience  Reply With Quote

4. Member Join Date
Sep 2009
Posts
4
Rep Power
0

## Heres what I wanted to do.... i figured it out

import java.util.Scanner;

public class Digits
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a 5 digit number:");
int x = keyboard.nextInt();
int fi,se,th,fr,ft;

fi = (x - x%10000) / 10000;
x = x%10000;
se = (x - x%1000) / 1000;
x = x%1000;
th = (x - x%100) / 100;
x = x%100;
fr = (x - x%10) / 10;
x = x%10;
ft = x;

System.out.println(fi + se + th + fr + ft);
}
}

Thanks very much though...You have a good day.  Reply With Quote

5. Member Join Date
Sep 2009
Posts
13
Rep Power
0

## You may want to think about the names you give the variables.

fi,se,th,fr,ft are all very well and do work, but it is not that obvious what they are.

It would be much better to use:

firstDigit, secondDigit, thirdDigit, fourthDigit, fifthDigit.

It will make the code that much easier to read, and you will thank yourself for it one day when you have to go back and re read your own code.  Reply With Quote

6. Member Join Date
Sep 2009
Location
Chennai
Posts
1
Rep Power
0

## % returns the remainder in a division problem. Getting user input in java through command line is not recommened. For now enter the input to the program directly rather than making the program dynamic. Java greatest strength is GUI . Dont worry you will learn about them later as you proceed through the course. If you want to know about GUI i will recommend Java how to program 6th edition Deitel &Deitel chapters 12 and 22 or 23  Reply With Quote

digit, int, java, program 