Results 1 to 6 of 6
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default Program to read integers until a specific number?

    Hello!

    I have a problem. I want to make a program that reads all the integers that are typed in and when a certain integer is typed in (lets say 0) it stops and then adds them all toghether.

    I was thinking of a while and for loop. And to place the integers to an array. But how do i add integers to an array? How can I do so that the array list keeps expanding if there are to few slots for all the integers?

    What would you guys suggest?

    Thanks.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default

    why do you want to use an array? you can add the integers directly or?

    pseudo:
    int n : 0
    int in :
    int sum :
    while(in = readInt != n){
    sum += in;
    }
    print sum

    :confused:
    Last edited by eRaaaa; 11-17-2010 at 04:48 PM.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    11

    Default

    use the class Scanner for reading ints and its methods hasNextInt() and nextInt() in the loop. like mentioned above you can add it directly.

  4. #4
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Sorry to be so bad guys... But I can't figure out how to write the loop...


    PHP Code:
    import java.util.Scanner;
    public class Ovn9
    {
    
    	public static void main(String[] args)
    	{
    		Scanner sc = new Scanner(System.in);
    		int n = 0;
    		int sum; 
    		int in;
    	while(
    			readInt(in != n)
        	 int in = sc.nextInt();
        	 {
        	 
        	 sum += in;
        	 }
    
        	 {
    		System.out.println(sum);
    
    
        	 }
    	
    } 
    	}
    	
    }

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default

    I never write here pseudocode again, that only confuse :)

    the readInt was a synonym for the nextInt or any other methods which can read integers.

    maybe a do-while loop is easier to understand for you ?!

    Java Code:
    	
    		Scanner sc = new Scanner(System.in);
    		int n = 0;
    		int sum = 0;
    		int in;
    		do {
    			//read next int to in, and after that, add in to sum.
    		} while (in != n);

  6. #6
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Finally got it to work, thanks for the help!

    PHP Code:
    import java.util.Scanner;
    public class Ovn9
    {
    
    	public static void main(String[] args)
    	{
    		Scanner sc = new Scanner(System.in);
    		int n = 0;
    		int sum = 0;
    		int in = 1;
    		
    		 while (in != n) {
    			 System.out.println("Type in an integer: ");
    		in = sc.nextInt();
    		 sum += in;
    		 }
    		System.out.println(sum);
        	 }
    	
    
    	}

Similar Threads

  1. Replies: 5
    Last Post: 12-06-2010, 12:43 AM
  2. Replies: 4
    Last Post: 11-03-2010, 06:17 PM
  3. read integers from txt
    By genji in forum New To Java
    Replies: 12
    Last Post: 05-27-2010, 09:33 PM
  4. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 10:40 PM
  5. Replies: 4
    Last Post: 03-28-2009, 04:58 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
  •