Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default newbie guidance.

    Good Day All!!! i want this program to read an interger , and print the sum of all positive #'s numbers within it. Im Stummped once again.

    Java Code:
    import java.util.Scanner ;
    public class Kush  {
    public static void main(String[]args)  
        {
        int j, holder;
        Scanner b = new Scanner(System.in);
        System.out.print("PLease enter a number : ");
        j = b.nextInt();
        holder = j;
        
    
    
    
    
        }
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    There are a few ways to acomplish this task. The first that springs to mind is the standard mathematical method, I'll just write it up in pseudocode:
    Java Code:
    start loop
      get the last digit with the mod operator
      add last digit to sum
      divide number by 10
      if number is zero, stop looping
    end loop
    Also, if you try to write this method recursively, it'll end up being 2 lines long.
    Next, you could also input your number as a String, and convert it to an int one character at a time, thus removing the need for mathematics apart from summing. See the API for the String class if you decide to go that route, you'll find all that you need there.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Wink

    Am i on the right path?

    Java Code:
    public static void main(String[]args)  {
    
    
    int sum = 0;
    int number = 0;
    int num1,num2,num3,num4,num5;
    Scanner scan = new Scanner(System.in);
                        System.out.println("PLease enter a five-digit interger");
                          int j = scan.nextInt();
                      
    while ( number >= 0) {
    num1 = j  % 10;
    sum = num1;
    
    System.out.println(num1);
    
            }
        }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Well so far i have extracted all the number not sure as to what else i have to do...ANYWAYS HERES WHAT I HAVE SO FAR

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class ne  {
    public static void main(String[]args)  {
    
    
    int sum = 0;
    int number = 0;
    int num1= 0;
    int num12 = 0;
    int num2= 0;
    int num13 =0;
    int num3 =0;
    int num14 = 0;
    int num4 =0;
    int num15 =0;
    int num5 = 0;
    int num16 =0;
    Scanner scan = new Scanner(System.in);
                        System.out.println("PLease enter a five-digit interger");
                          int j = scan.nextInt();
                      
    if  ( number >= 0) {
    num1 = j  % 10;
    num12 = j /10;
    
    num2 = num12 % 10;
    num13 = num12 /10;
    
    num3 = num13 %10;
    num14 = num13/10;
    
    num4 =num14 % 10;
    num15 = num14 /10;
    
    num5 = num15 %10;
    num16 = num15/10;
    
    sum = 0 +  num1+num2+num3+num4+num5;
    }
    System.out.println(num1);
    System.out.println(num2);
    System.out.println(num3);
    System.out.println(num4);
    System.out.println(num5);
    
            }
    }

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Take a look at my pseudocode again, note the first line start loop!
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    karthikaa is offline Member
    Join Date
    Oct 2010
    Location
    Pondicherry
    Posts
    6
    Rep Power
    0

    Default

    i guess this code would help u...
    Java Code:
    public static void main(String[] args) {
    		int sum = 0;
    		int num = 0;
    		int n;
    		Scanner scan = new Scanner(System.in);
    		System.out.println("PLease enter a five-digit interger");
    		int j = scan.nextInt();
    		n = j;                
    		while ( n > 0) {
    			num = n  % 10;
    			sum = sum + num;
    			n /= 10;
    		}
    
    		System.out.println(sum);
    	}

  8. #8
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    im not sure how to put the whole thing in a loop. i know its the same calculations over 5 times but dont no how to translate it into a loop.Thx:confused:

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Since karthikaa already posted the solution for you, I'll [sarcasm] help [/sarcasm] more by providing something you probably won't understand but will give you a good grade.
    Java Code:
    public int sumOfDigits(int num) {
      if(num == 0) return 0;
      return num%10+sumOfDigits(num/10);
    }
    In short, providing answers is not the way to help people in the long run. Not directed at Bgreen7887.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Solved thanks! Sorry if i dont always understand , on the first go around. I am a rookie, im Learning from my Mistake. I just want to get better. Appreciate all the HELP

  11. #11
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Like I said, the derision in my post wasn't aimed at you, but at the guy who posts solutions. Getting to grips with the basics is what gives you a good foundation for learning more advanced topics, someone spoonfeeding you solutions is only detrimental to your learning process.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  12. #12
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    I understood and agree with you. I've been told that programming is not what you but is how you think..Is there any truth to that statement?

  13. #13
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Yes, pretty much. Learning to program has a lot to do with translating your thought process into an algorithm the computer can understand, regardless of the language you're learning. Also, each problem you face, try to solve it in a few different ways, and compare the results. Do all the methods return the same result (ie all function equivalently), how about the speed at which your algorithm solves a certain problem? Toying around like this is what gets you into the state of mind of a programmer.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  14. #14
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Ok because i've been programming for about a month. I just started school for it. Sometime the hardest problem for me is to understand exactly what the question are asking for and then getting started! I know i give the time because programming is literally the only thing do now. I just can't see any huge improvements in myself since then .

  15. #15
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    A good example of tunnel vision solutions is your code snippet. You made a program that can sum the digits of a 5 digit number. What about 6, 7 and more digits? A similar example would be, to make a program that computes the 10th link of a fibonacci sequence. Are you going to make an algorithm that can find only the 10th element just so you can finish an assignment, or are you going to make an algorithm that can compute the n-th element and be done with the problem forever?
    In summation, do many problems, try to find a generic solution that works on many cases, not just the one present in the assignment. Once you're more confortable with the basics, definately check out recursion. It's the one thing the really opened my eyes, and I've been making leaps in my learning process since.
    Last edited by m00nchile; 10-17-2010 at 08:00 PM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  16. #16
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    LOL your last reply made me laugh , but hit home your totally right. im not sure if this is normal but i usually get stumped on every code. maybe i'll go back to first chap of my book doing all the programming projects and increment like that. until im concurrent. :)

  17. #17
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Hey, noone is born with all the knowledge in the world. Just keep plugging away, it'll all start to make sense soon :D
    Ever seen a dog chase its tail? Now that's an infinite loop.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by m00nchile View Post
    Hey, noone is born with all the knowledge in the world.
    Indeed, I was born stupid too but I fell on my head when I was a baby; from then on my mother told me that I wasn't a normal person ;-)

    kind regards,

    Jos

  19. #19
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    "Special" can go both ways :D
    Ever seen a dog chase its tail? Now that's an infinite loop.

  20. #20
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    you guys are crazy i'd say i was weird not "special" lol ..why am i getting compile error with this so far?
    Java Code:
    import java.util.Scanner;
    public class ap  {
    public static void main(String[]args)  { 
    
    String n;
    Scanner jennifer = new Scanner(System.in);
    System.out.println("Please enter a Sentence:");
    n = jennifer.nextLine();
    while (jennifer.hasNext() ) {
            System.out.println(jennifer.nextLine);
             }
    
         }
    }   // shouldn't it print user text on sentence per line

Page 1 of 2 12 LastLast

Similar Threads

  1. Guidance for SCJP
    By sairam in forum New To Java
    Replies: 1
    Last Post: 06-30-2010, 10:21 AM
  2. Guidance needed
    By Curtiz in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2010, 06:41 PM
  3. need guidance in component placement
    By rdtindsm in forum AWT / Swing
    Replies: 2
    Last Post: 05-10-2009, 05:14 PM
  4. Guidance in writing a compiler
    By Jeremy in forum Advanced Java
    Replies: 2
    Last Post: 11-29-2008, 12:25 AM
  5. Guidance
    By gizdev in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 08:14 PM

Posting Permissions

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