I am trying to read in a file that holds an unknown number of x and y coordinates that are to be later plotted into a 2-D array. Does the file have to be read through twice? Once to determine size of the array needed to plot all coordinates, then a second time to actually plot them, or can this all be done in one step?

import java.io.*;
import java.util.Scanner;

public class patternRec {
    public static void main (String[] args){          
        String FileName = "data.dat";
        readFile(FileName);
        
    }
    
    public static void readFile(String f){
        int x=0,y=0;
        
        try{
        
            Scanner input = new Scanner(new File(f));
            while(input.hasNextInt())
            { 
               x = input.nextInt();
               y = input.nextInt();
               System.out.println("x: " + x + " y: " +y);
               
               if(x != 33)
               {
                   inputStroke(x,y);
               }   
            }        
        }catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }
    }
    
    public static void inputStroke(int x_val,int y_val){
        array[x_val][y_val] = 1;
    }

    public static void printStroke(int [][]array){
        for(int i = 0; i< arraysize; i++){
            for(int j=0;j< arraysize; j++){
                if(array[i][j] == 1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }System.out.println();
        }
    }
    
}

holds an unknown number

If you don't know the number of pairs and must use an array, then two passes might be needed. A better approach would be to use an ArrayList which will automatically resize if needed.

Thank you for the suggestion! After learning about and implementing ArrayLists, it simplified things greatly. All data has now been inputted into two arrayLists, one for X-coordinates, and one for Y-coordinates. Now I have encountered another problem. I am trying to create a character array to contain the image and print it out. Its size in the x-direction is the size of a character * number of characters. This, when printed, would produce a picture with all characters next to each other. I am getting an array out of bounds error after imputing the first set of numbers. The bottom if-statement tests if the x and y values are 33, if they are not, it should input the values. If they are it should do nothing and go on to next iteration. So why would I get an out of bounds error?

// Method to read coordinates into two arrayLists
    public static void readFile(String f, ArrayList x, ArrayList y){
        int num=0;
        String numString = "";
        
        try{
            Scanner input = new Scanner(new File(f));
            while(input.hasNextInt())
            { 
               num = input.nextInt();
               numString = Integer.toString(num);
               x.add(numString);
               if(num > Xhighest && num < 33)
                   Xhighest = num;
               num = input.nextInt();
               numString = Integer.toString(num);
               y.add(numString);
               if(num > Yhighest && num < 33)
                   Yhighest = num;
            }        
        }catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }
    }
    
    public static void draw(ArrayList x, ArrayList y){
        int spacer = Xhighest;
        int multiplier = 0;
        char[][] picture = new char[Xhighest*getNumberCharacters(x)][Yhighest];
        for(int i = 0; i<x.size();i++){
            Integer xVal = Integer.parseInt(x.get(i).toString());
            Integer yVal = Integer.parseInt(y.get(i).toString());
            
            if(xVal !=99 && yVal != 99){
                spacer = Xhighest*multiplier;
                if(xVal != 66 && yVal != 66){            
                    if(xVal !=33 && yVal != 33){
                        picture[xVal+spacer][yVal] = '*';
                        System.out.println(xVal+spacer + " " + yVal);
                    }else{}
                }else{multiplier++;}
            }
        }
        print(picture);
    } 
}

I am getting an array out of bounds error

Please post the full text of the error message. It has useful info for working on the problem.

What statement is throwing the exception? What is the value of the index that is out of bounds? What is the size of the array?

The file being used has a highest value of 19 for both X and Y, and there are 9 characters to be printed. Therefore the character array is 19*9x19 or 171x19. When encountering a 33, 66, or 99 in the data, the program should ignore and go to next iteration, which i thought was handled with the 3 if statements. Line# 39 above throws the exception.

OUTPUT:

X Highest : 19, Y Highest 19
Character count = 9
Character count = 9
7 2
6 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 19
5 2
4 2
3 2
2 2
1 2
at patternRec.draw(patternRec.java:68)
at patternRec.main(patternRec.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Look at the statement at line 68. What index has a value past the end of the array?
If the array has 19 elements, then 18 is the largest index possible.
Remember array indexes start at 0 and go to the length-1

Move the println statement at line 40 to before the statement with the array reference so you can see the value of the variables used as indexes before the exception happens.

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.