i have an integer like this:1
4
3
2
1
1
3
8
1
1
1
1
1
2
1
6
2
3
1
12
what can i do to add each individual number together like this: 4+3+2+1+1+1+3+8 etc?
gibson.nathan 0 Junior Poster in Training
Recommended Answers
Jump to Postsir according to me
u have to push all numbers in a stack and thn using plus opretor pop out these all thn u will get sum of all numbersNo stack is required to calculate the sum. Set up a sum variable and initialize it to 0. Read in …
Jump to PostThat would depend on the details you haven't supplied - such as where this data is coming from.
Jump to Postint sum = 0; while (inFile.hasNext()) { token = inFile.next( ); int num = Integer.parseInt(token); int nums = in.nextInt(); sum = nums + nums; int average = sum/numTrials; System.out.println(average); } inFile.close();
would this be right?
You tell us. Does it run? Do you get the right results? Short …
Jump to Postbesides what happens in or out of the loop, are my statements right? that was my main concern, i can figure out what goes where in the loop.
That's not a good approach to programming. A line is correct if it's the right line AND it's in the right spot. …
All 15 Replies
jmaat7 0 Light Poster
ajeet choudhary 0 Newbie Poster
cgeier 187 Junior Poster
VernonDozier 2,218 Posting Expert Featured Poster
gibson.nathan 0 Junior Poster in Training
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
gibson.nathan 0 Junior Poster in Training
VernonDozier 2,218 Posting Expert Featured Poster
gibson.nathan 0 Junior Poster in Training
santosh.garg 0 Newbie Poster
gibson.nathan 0 Junior Poster in Training
santosh.garg 0 Newbie Poster
VernonDozier 2,218 Posting Expert Featured Poster
gibson.nathan 0 Junior Poster in Training
VernonDozier 2,218 Posting Expert Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.