Results 1 to 5 of 5
- 11-18-2012, 09:08 PM #1
Member
- Join Date
- Oct 2012
- Location
- ph
- Posts
- 43
- Rep Power
- 0
for in while loop and arraylist advanced for
1)my first problem is that i want to make the input in my while loop print out score1,score2,etc until the user inputs a non integer value but i dnt knw wat to put a place of 50 to solve my problem
2)the advanced for loop gives a syntax error that says "incompatible types,reuired int ..found java.lang.object"..and i need to print the value of the total calculations i have done
Java Code:import java.util.*; public class Sumcalculator { public static void main(String[] args) { ArrayList scores1=new ArrayList(); Scanner input=new Scanner(System.in); System.out.println("TYPE IN THE SCORES OF THE FIRST STUDENT:"); System.out.print("score 1: "); while(input.hasNextInt()) { for(int i=2;i<50;i++) //i dont know what to write in place of 50!! { int stud1=input.nextInt(); System.out.printf("%s %d%s","score",i,":"); scores1.add(stud1); } System.out.print(scores1); for(int nums: scores1) //doesnt advanced for loop work for array list? { int total=total+nums; } }
THANKS TO ALL MEMBERS OF THIS SITE FOR THEIR PREVIOUS HELP!!...luv u all!
GREAT JOB ADMIN!!
- 11-19-2012, 03:02 AM #2
Re: for in while loop and arraylist advanced for
Please spell words in full. This is a technical forum, not SMS chat.
Recommended reading
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-19-2012, 09:20 AM #3
Member
- Join Date
- Oct 2012
- Location
- ph
- Posts
- 43
- Rep Power
- 0
Re: for in while loop and arraylist advanced for
i cant re edit with my phone....pls help me...
- 11-19-2012, 10:36 AM #4
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
Re: for in while loop and arraylist advanced for
Think that is what you are looking for, and no need for the for loop as it will exit on non-integer entries.Java Code:package testing; import java.util.*; public class Sumcalculator { public static void main(String[] args) { ArrayList<Integer> scores1=new ArrayList<Integer>(); Scanner input=new Scanner(System.in); System.out.println("TYPE IN THE SCORES OF THE FIRST STUDENT:"); int i = 2; System.out.print("Score 1: "); while ( input.hasNextInt() ) { System.out.printf("Score %d%s ",i,":"); scores1.add(input.nextInt()); i++; } System.out.print(scores1); int total = 0; for(int nums : scores1){ total += nums; } System.out.println("Total is : " + total); } }
- 11-20-2012, 03:26 PM #5
Member
- Join Date
- Oct 2012
- Location
- ph
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
arrayList in a while loop
By Lund01 in forum New To JavaReplies: 3Last Post: 11-06-2011, 11:28 PM -
How to run a for-each loop through an ArrayList?
By simon.black325 in forum New To JavaReplies: 6Last Post: 09-08-2011, 04:18 AM -
Problems with a loop calling data from an ArrayList.
By moriarty in forum New To JavaReplies: 30Last Post: 03-28-2010, 02:00 AM -
Iterating through ArrayList using For loop
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:53 AM -
Iterating through ArrayList - traditional for loop
By Java Tip in forum Java TipReplies: 0Last Post: 11-14-2007, 03:22 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks