Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Jingoism

Thread: for in while loop and arraylist advanced for

  1. #1
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default 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!!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: for in while loop and arraylist advanced for

    Quote Originally Posted by nonybrighto View Post
    1)... but i dnt knw wat to put ...
    Please spell words in full. This is a technical forum, not SMS chat.

    Recommended reading

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: for in while loop and arraylist advanced for

    i cant re edit with my phone....pls help me...

  4. #4
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: for in while loop and arraylist advanced for

    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);
    
    	}
    
    }
    Think that is what you are looking for, and no need for the for loop as it will exit on non-integer entries.
    nonybrighto likes this.

  5. #5
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: for in while loop and arraylist advanced for

    thanks a lot bro...it really helped...you wrote the code completely...you are good

Similar Threads

  1. arrayList in a while loop
    By Lund01 in forum New To Java
    Replies: 3
    Last Post: 11-06-2011, 11:28 PM
  2. How to run a for-each loop through an ArrayList?
    By simon.black325 in forum New To Java
    Replies: 6
    Last Post: 09-08-2011, 04:18 AM
  3. Problems with a loop calling data from an ArrayList.
    By moriarty in forum New To Java
    Replies: 30
    Last Post: 03-28-2010, 02:00 AM
  4. Iterating through ArrayList using For loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:53 AM
  5. Replies: 0
    Last Post: 11-14-2007, 03:22 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
  •