6 Replies - 3017 Views - Last Post: 26 April 2011 - 08:16 PM Rate Topic: -----

#1 Maxis0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 11

Array list- print in reverse, maxValue, and total ints entered.

Posted 17 April 2011 - 06:21 PM

Hey all,
I am just digging into arrays and array lists and I've come across a problem that I cannot figure out. My goal here is to create an array list of size 20 to be filled(or not) by a user inputting non-negative integers or -1 to end. I also need experience in finding the average of these input as well as returning them (preferably in reverse order). Here is the mess of a code I have so far, the maxVal seems to print fine. However, I don't want -1 to be counted in the total number of ints entered. I would prefer to not use breaks in the code as I am still a beginner and have not dealt with them yet. Any help would be GREATLY appreciated!

import java.util.*;

public class ArrayIntList
{
    public static void main(String[] args)
    {
        int total=0;
        int[] value = new int[20];
        int userIn=0; int maxVal=0; int sum=0;
        double avg=0;
        Scanner scan = new Scanner (System.in);
        System.out.println("Give me a non-negative integer. Enter -1 to end.");

        for (total=0; userIn>=0;)
        {
            if (userIn>=0)
            {
                userIn = scan.nextInt();
                if (userIn>maxVal)maxVal = userIn;
                sum = userIn + sum; //not sure what to assign to this ://>
                total++;
                avg = (sum)/(total); //avg is calculating with the -1 used to end the program
            }
        }    
        if (userIn<0)
        {
            System.out.println("You entered " + total + " integers");
            System.out.println("The average of the integers is " + avg);
            System.out.println("The highest integer is " + maxVal);
            System.out.println("Done.");
        }
    }
}



After reading around I see the use of this for loop to print in reverse but I cannot get it to work anywhere in my code.

for (int i = array.length - 1; i>=0; i--)
{
    System.out.println(" " + i + " " + array[i]);
}



Thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: Array list- print in reverse, maxValue, and total ints entered.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array list- print in reverse, maxValue, and total ints entered.

Posted 17 April 2011 - 06:27 PM

for(int i = array.length; --i > 0;)

should work :)
Was This Post Helpful? 1
  • +
  • -

#3 Maxis0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 11

Re: Array list- print in reverse, maxValue, and total ints entered.

Posted 17 April 2011 - 06:44 PM

View Postpbl, on 17 April 2011 - 06:27 PM, said:

for(int i = array.length; --i > 0;)

should work :)


Thanks for your help pbl, that did allow me to print in reverse however, this is what's returning
Give me a non-negative integer. Enter -1 to end.
99
100
98
-1
 19 0
 18 0
 17 0
 16 0
 15 0
 14 0
 13 0
 12 0
 11 0
 10 0
 9 0
 8 0
 7 0
 6 0
 5 0
 4 0
 3 0
 2 0
 1 0
You entered 4 integers
The average of the integers is 74.0
The highest integer is 100
Done.


This is on the right path but I would like to print the integer values the user enters in reverse... Any other suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 Maxis0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 11

Re: Array list- print in reverse, maxValue, and total ints entered.

Posted 17 April 2011 - 07:44 PM

Ok so I got everything working except I'm still not able to print the values that the user enters. Here is what I have now. Please help I have tried everything I could come up with on my own.
public class Arrays7 
{
    public static void main(String[] args)
    {
        int value[] = new int[20];
        Scanner scan = new Scanner (System.in);
        System.out.println("Give me a non-negative integer. Enter -1 to end.");
        double userIn=0;
        double maxVal=0; double sum=0; int total=0;
        for (total=0; userIn>=0;)
        {
            if (userIn>=0)
            {
                userIn = scan.nextInt();
                if (userIn>maxVal)maxVal = userIn;
                sum += userIn;
                total++;
            }
        }    
        double average = (sum - value[total])/(total - 1);
        if (userIn<0)
        {
            System.out.println("You entered "+(total-1)+" integers");
            System.out.println("The highest integer was "+ maxVal);
        }
        System.out.println("The average of the integers is "+ average);
        for (int i = value.length; --i > 0;)
        {
            System.out.println(i + value[total]);
        }
        System.out.println("Done.");
    }
} 


Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Array list- print in reverse, maxValue, and total ints entered.

Posted 17 April 2011 - 08:17 PM

I think you are approaching this incorrectly. You need to input until EITHER -1 OR the array has been filled. Right now, you can fill 100 times without stopping. This later causes severe issues with your design. I'd break your code up more like this:

public class Arrays7 
{
    public static void main(String[] args) {
        int value[] = new int[20];
        Scanner scan = new Scanner (System.in);

        // Input all the numbers into the array
        int numFilled = 0;
        int input = 0;
        for (int i = 0; i < value && input >= 0; i++) {
            input = scan.nextInt();
            value[i] = input;
            numFilled++;
        }
    
        // Now get total, and average
        int total = getTotal(value,  numFilled);
        double av = getAverage(total, numFilled);

        // Print array backwards
        for (int i = numFilled-1; i >= 0; i--) 
            System.out.println(i + " - " + value[i];
    }


    // Implement total and average methods here.
}


Was This Post Helpful? 3
  • +
  • -

#6 Maxis0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 11

Re: Array list- print in reverse, maxValue, and total ints entered.

Posted 26 April 2011 - 01:19 PM

Got it! I bought a textbook to look a little further into it and turns out I was just making a mess but props to Ninja for that fragment it helped me out big time. Thanks to all for the help!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array list- print in reverse, maxValue, and total ints entered.

Posted 26 April 2011 - 08:16 PM


for (total=0; userIn>=0;)   // this for() check for userIn >= 0
{
     if (userIn>=0)   // no need to check again here. If you are here it is because userIn>=0


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1