Results 1 to 10 of 10
  1. #1
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Question Trouble implementing a solution plz help

    First of all I would like to say I've spent about 2 hours trying to solve this on my own and I failed. I'm not just asking for you to do my work for me. I tried one implementation using an ArrayList and another using two scanner objects , but both attempts were unsuccessful.

    I have the following assignment

    Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the inputted numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:

    The minimum integer is 0
    The sum of the positive integers is 0
    The sum of the even integers is 0
    The number of negative integers in the sequence is 0


    This means that using all numbers your program reads (including the last number 0), you need to compute the minimum, the sum of only positive integers (integers that are greater than 0), the sum of even integers (integers that can be divided by 2, you can use "number%2 == 0"), and count how many negative integers (integers that are less than 0) in the sequence.

    Note that the above is an output for the first test case. For other test cases, you will have different numbers.

    Do not output a prompt to query for the numbers. The number 0 is included in the sequence of integers and should be included in all of your calculations.

    As I briefly mentioned, I tried to accept the numbers from the user as one long string and then construct an array from that using a scanner object. I also tried storing the values in an ArrayList (my first experience with ArrayLists) and then using the x.toArray() method to construct an int array. Both techniques led to the compiler regurgitating countless error messages my way.

    What's the best way to implement this?

    Thanks.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Scanner class

    I think you need to read up about the scanner class:
    Scanner (Java Platform SE 6)
    Study the examples and pay special attention to the nextInt() method
    You're idea about using arrayLists is correct since you don't know how many elements there are going to be.
    You will need to use a "for" loop to populate the arraylist...
    Java Code:
    for ()
    {
      myArray[i] = scanner.nextInt();
    }
    or something close to that ... you can fill in the blanks.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    why do you need to use arraylist? the assignment didn't ask you to keep track of all the int's entered.
    Java Code:
          while(!(n==0)){
             // sum stuff here
             sum += n;
             n = input.nextInt();
          }
    n, sum = int
    input = scanner
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by angryboy View Post
    why do you need to use arraylist? the assignment didn't ask you to keep track of all the int's entered.
    What I need to output is
    The minimum integer is 0
    The sum of the positive integers is 0
    The sum of the even integers is 0
    The number of negative integers in the sequence is 0
    I do need to store the numbers to be able to calculate those four answers.

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    is it something like this?
    Java Code:
    S:\SVN\chase>java Summer
    Enter Integer. (enter 0 to stop)
    -------------------------------------------------------
    3 5 22 1 9
    2 5 8 9 -1 15 -3
    7 8 4 -2 0
    -------------------------------------------------------
    The minimum integer is -3
    The sum of the positive integers is 98
    The sum of the even integers is 42
    The number of negative integers in the sequence is 3
    I just used 4 int variables to hold the sum. and update it inside the while loop.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default

    Here is my attempt

    Java Code:
    // Assignment #: 2
    import java.util.Scanner; //Used to accept the input intergers
    
    public class Assignment2 {
            
        public static void main(String[] args) {
            
            int min = 0, psum = 0, esum = 0, ncount = 0; //Used to store the final output values. Updated in the while loop as each int is entered.
            
            Scanner scan = new Scanner(System.in);
            
            while (scan.hasNextInt()) {
            	if (scan.nextInt() == 0)
            	{
            		break;
            	}
            	else if (scan.nextInt() < min)
            	{
            		min = scan.nextInt();
            	}
            	else if (scan.nextInt() > 0)
            	{
            		psum += scan.nextInt();
            	}
            	else if (scan.nextInt() % 2 == 0)
            	{
            		esum += scan.nextInt();
            	}
            	else if (scan.nextInt() < 0)
            	{
            		ncount++;
            	}
            }
            
            System.out.println("The minimum interger is "+min);
            System.out.println("The psum is "+psum);
            System.out.println("The esum is "+esum);
            System.out.println("The ncount is "+ncount);
        }
    }
    but the output is always

    The minimum interger is 0
    The psum is 0
    The esum is 0
    The ncount is 0
    regardless of the numbers entered.

    What's wrong with my code? Thanks.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are getting and discarding input every time you call scan.nextInt. You want to call this method once in the while loop, right at the beginning:

    Java Code:
    whilte (scan.hasNextInt())
    {
      int nextInt = scan.nextInt(); // call this once!
      scan.nextLine();  // often useful to "swallow" the end of line char
      if (nextInt == .....) // now do your tests on the int variable
      { 
         //.... etc...

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    looking good man. couple of things to fix.

    first, min should not be initialized to 0. it should be something large like 999999. what's the largest value for int before overflow? (hint: take a look at java.lang.Integer)

    second, don't use else if, just use if statments.

    lastly, fubarable has spoken it already...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default

    I got everything fixed and it works great thanks everybody.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Trouble implementing part of code into GUI
    By Flaresplitz in forum New To Java
    Replies: 1
    Last Post: 12-21-2008, 07:51 AM
  2. Please give me a solution
    By ivvgangadhar in forum AWT / Swing
    Replies: 5
    Last Post: 11-14-2008, 01:43 PM
  3. solution for my project
    By shkelqa in forum AWT / Swing
    Replies: 4
    Last Post: 05-28-2008, 10:31 PM
  4. solution for my project
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-21-2008, 01:03 PM
  5. Please need solution
    By prithvi in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 01:27 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
  •