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
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!!
Re: for in while loop and arraylist advanced for
Quote:
Originally Posted by
nonybrighto
1)... but i dnt knw wat to put ...
Please spell words in full. This is a technical forum, not SMS chat.
Recommended reading
db
Re: for in while loop and arraylist advanced for
i cant re edit with my phone....pls help me...
Re: for in while loop and arraylist advanced for
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);
}
}
Think that is what you are looking for, and no need for the for loop as it will exit on non-integer entries.
Re: for in while loop and arraylist advanced for
thanks a lot bro...it really helped...you wrote the code completely...you are good