Question

I'm trying to implement A* algorithm in java. The user is requested to type the width and height of the grid-to-be. My problem is in validating that input. Here is my code:

public class Map extends java.awt.Panel implements Serializable
{
JFrame frame = new JFrame();
String rows =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό γραμμών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);
String cols =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό στηλών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);

int rowsnum = Integer.parseInt(rows); 
int colsnum = Integer.parseInt(cols);
GridCell gridCell[][] = new GridCell[rowsnum][colsnum];

public Map()
{
        super();

    setLayout(new GridLayout(rowsnum,colsnum)); 
    for(int i=0;i<rowsnum;i++){ 
        for(int j=0;j<colsnum;j++){
           gridCell[i][j] = new GridCell();
           gridCell[i][j].setPosition(new Point(i,j));
           add(gridCell[i][j]);
        }
    }
}

I've tried to check the input in a new method, but my problem is that I need to have access to rowsnum , colsnum and gridcell[] from the other classes of my program.

I'm new in Java and I'd very much appreciate any help. :-)

Was it helpful?

Solution 3

Simplest solution i could think was

Validating width/height numbers with If

public class Map extends java.awt.Panel implements Serializable
    {      
    String width =  JOptionPane.showInputDialog("Give Width (>5 , <30)");
    int w = Integer.parseInt(width);

    String height =  JOptionPane.showInputDialog("Give height (>5 , <30)");
    int h = Integer.parseInt(height);

    GridCell gridCell[][] = new GridCell[w][h];

    public Map()
    {
    super();
    setLayout(new GridLayout(w,h)); 

    if ( w < 5)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( w > 30)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( h < 5)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( h > 30)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}


        for(int i=0;i<w;i++){ 

            for(int j=0;j<h;j++){
               gridCell[i][j] = new GridCell();
               gridCell[i][j].setPosition(new Point(i,j));
               add(gridCell[i][j]);
            }
        }
    }

Validating string input error with try-catch on public main

public static void main(String[] argv) {

try{    
AStar2 test = new AStar2();                   
test.setLocationRelativeTo(null);
test.setDefaultCloseOperation(test.EXIT_ON_CLOSE);}
catch(NumberFormatException e){JOptionPane.showMessageDialog ( null, "BLOODY HELL DONT PUT STRINGS FOR INPUT, APPLICATION IS CLOSING , TRY BETTER NEXT TIME" );}

    }

}

OTHER TIPS

Right now your rowsnum, colsnum, and GridCells are variables within your "Map" class.

In order to access this as instance variables from other classes,

public int rowsnum = // something
public int colsnum = // something
public GridCell girdCell[][] = //something

"public" means the variable can be accessed from outside the class.

and outside your Map class,

Map m = new Map();
int rowsnum = m.rowsnum
int colsnum = m.colsnum
// access instance variables

However, it is better to create getters and setters for your instance variables.

That is, to create separate methods for getting and setting these variables.

public int getRowsNum() {
    return rowsnum;
}

and such.

you can access them through an Map class object (change access modifier to public of those variables )

Example : in class B

public class Test {
    public static void  main(String args[]){
        Map someMap = new Map();

        //Access gridCell 
        someMap.gridCell;

        //Access rowsnum
        someMap.rowsnum;

        //Access colsnum
        someMap.colsnum;
    }
}

Or you can made those as static and access them with class name

Example : Map.rowsnum;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top