# newbie guidance.

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-16-2010, 11:40 PM
Bgreen7887
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.

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;

}
}

• 10-17-2010, 12:15 AM
m00nchile
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:
Code:

start loop
get the last digit with the mod operator
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.
• 10-17-2010, 04:19 PM
Bgreen7887
Am i on the right path?

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);
int j = scan.nextInt();

while ( number >= 0) {
num1 = j  % 10;
sum = num1;

System.out.println(num1);

}
}
}

• 10-17-2010, 05:06 PM
Fubarable
Quote:

Originally Posted by Bgreen7887
Am i on the right path?

Does it work when you test it?
• 10-17-2010, 05:34 PM
Bgreen7887
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

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);
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);

}
}

• 10-17-2010, 05:57 PM
m00nchile
Take a look at my pseudocode again, note the first line start loop!
• 10-17-2010, 06:19 PM
karthikaa
i guess this code would help u...
Code:

public static void main(String[] args) {
int sum = 0;
int num = 0;
int n;
Scanner scan = new Scanner(System.in);
int j = scan.nextInt();
n = j;
while ( n > 0) {
num = n  % 10;
sum = sum + num;
n /= 10;
}

System.out.println(sum);
}

• 10-17-2010, 06:20 PM
Bgreen7887
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:
• 10-17-2010, 06:25 PM
m00nchile
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.
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.
• 10-17-2010, 06:31 PM
Bgreen7887
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
• 10-17-2010, 06:35 PM
m00nchile
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.
• 10-17-2010, 06:38 PM
Bgreen7887
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?
• 10-17-2010, 06:42 PM
m00nchile
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.
• 10-17-2010, 06:49 PM
Bgreen7887
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 .
• 10-17-2010, 06:58 PM
m00nchile
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.
• 10-17-2010, 07:05 PM
Bgreen7887
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. :)
• 10-17-2010, 07:11 PM
m00nchile
Hey, noone is born with all the knowledge in the world. Just keep plugging away, it'll all start to make sense soon :D
• 10-17-2010, 07:30 PM
JosAH
Quote:

Originally Posted by m00nchile
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
• 10-17-2010, 07:57 PM
m00nchile
"Special" can go both ways :D
• 10-17-2010, 08:17 PM
Bgreen7887
you guys are crazy i'd say i was weird not "special" lol ..why am i getting compile error with this so far?
Code:

import java.util.Scanner;
public class ap  {
public static void main(String[]args)  {

String n;
Scanner jennifer = new Scanner(System.in);
n = jennifer.nextLine();
while (jennifer.hasNext() ) {
System.out.println(jennifer.nextLine);
}

}
}  // shouldn't it print user text on sentence per line

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last