0

So im working on this project for class and I am to read a text file called "randomNumbers" store it in an array and print it out to the screen. It complies fine but when I run the code I get

enter code here
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at projectFour.read_file(projectFour.java:32)
at projectFour.main(projectFour.java:13)

Iv tried to the best of my ability to solve it but I am stuck.

Here is the code I have thus far,

enter code here import java.io.File;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class projectFour
{
public static int [] global_numbers;


public static void main (String[] args)
{
  read_file();
  print_numbers(global_numbers);

}



public static void read_file()
{
try
      {
        File file = new File("randomNumbers.txt"); 
        Scanner scan = new Scanner(file);
        int maxNumberInFile = convertingStringToInt(scan.nextLine()); // read the first line which is 100 to set array size
        global_numbers = new int[maxNumberInFile]; // set the array size equal to the first line read which is 100
        int i = 0; 

             while(scan.hasNextLine()) // while there is a next line to read, do this...
              {
                 String line = scan.nextLine();
                 global_numbers [i] = convertingStringToInt(line); 
                 i++; 
              }
                 scan.close();        
      }

  catch(IOException ex)
        {
           ex.printStackTrace();
        }    

}

public static int convertingStringToInt(String number)
{  
try
     {
        return Integer.parseInt(number);
     }

  catch(NumberFormatException n)
     {
           return -460;
     }         
}

public static void print_numbers(int [] numbers)
{
  int max = numbers.length;
  for(int i = 0; i < max; i++)
     {
        System.out.println(numbers[i]); 
     }
}
}
SnakeEyes
  • 27
  • 5
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – David Mar 19 '17 at 00:51

3 Answers3

0

The code you have posted works perfectly without errors.

You just have to make sure that the number of lines (excluding the first line) is (100).

To prove that test it on a small scale for example:

Put this in randomNumbers.txt

10
1
2
3
4
5
6
7
8
9
10
Abdelrahman Wahdan
  • 2,056
  • 4
  • 36
  • 43
0

That's because there're more than 100 numbers in your file. If you only want to read 100 numbers, you can:

public static void read_file()
{
    try
    {
        File file = new File("randomNumbers.txt");
        Scanner scan = new Scanner(file);
        int maxNumberInFile = convertingStringToInt(scan.nextLine());   // read the first line which is 100 to set array size
        global_numbers = new int[maxNumberInFile]; // set the array size equal to the first line read which is 100
        int i = 0;

        //while(scan.hasNextLine()) // while there is a next line to read, do this...
        while (i < maxNumberInFile)
        {
            String line = scan.nextLine();
            global_numbers [i] = convertingStringToInt(line);
            i++;
        }
        scan.close();
    }

    catch(IOException ex)
    {
        ex.printStackTrace();
    }

}

If you don't know how many numbers in the file exactly, you can use a List to store the numbers.

Ke Li
  • 942
  • 6
  • 12
  • okay yeah thats what I thought. I came up with a for loop. now my problem is all my reutrn values are -460 so my converting string to int method isnt working.... – SnakeEyes Mar 19 '17 at 01:13
  • You can print the string to see if it's a valid int before convert it to int. Maybe the string contains some other characters? – Ke Li Mar 19 '17 at 01:20
0

Thanks, it is now reading the file, however in my convertingToStringMethod there is something not right as all the output values are -460, except the very last number in the file is read....

enter code here
  public static int convertingStringToInt(String numbers) //what does string       number equal? why/where is it declared?
 {  
  try
     {
        return Integer.parseInt(numbers);
     }

  catch(NumberFormatException n)
     {
           return -460;
     }         
}
SnakeEyes
  • 27
  • 5