I'm in an intro programming class and have a problem. Hoping someone can assist me. What I'm trying to do is import a data file and have it read the array in reverse order. That part I have. The second thing I'm trying to do is have it total the numbers in the array, which are basically {99, 88, 77, 66, 55, 44, 33, 22, 11, 0}, but the book we have is horrible. It doesn't explain things well, and everything I've tried gives me an ArrayIndexOutOfBoundsException.

// Application Reverse reads numbers into an array 
// and prints them out in reverse order.   
import java.util.Scanner;
import java.io.*;
public class Reverse
{ 
  public static void main(String[] args) throws IOException
  {
    final int MAX = 10;
     Scanner inFile = 
      new Scanner(new FileReader("Reverse.dat"));
    int[]  numbers;
    numbers = new int[MAX];
    int  index;
    int sum;
           
     for (index = 0; index < numbers.length; index++)
    {
      numbers[index] = inFile.nextInt();
     }
    
     for (index = MAX - 1; index >= 0; index--)
     System.out.println(numbers[index]);
       
   inFile.close();
  }
}

Recommended Answers

All 2 Replies

The second thing I'm trying to do is have it total the numbers in the array, which are basically {99, 88, 77, 66, 55, 44, 33, 22, 11, 0},

use a loop that will add the value of the current array index

I got it. Thanks that helped a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.