Results 1 to 6 of 6
  1. #1
    moe123 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Exclamation 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.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    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
    Ramya:cool:

  3. #3
    moe123 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    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

  4. #4
    moe123 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  5. #5
    JavaVideos is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default

    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.

  6. #6
    althor is offline Member
    Join Date
    Sep 2009
    Location
    Chennai
    Posts
    1
    Rep Power
    0

    Default

    % 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

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  3. Replies: 10
    Last Post: 01-29-2009, 08:00 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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