Im currently working on a Connect4 progrqam and I'm trying to convert a string into an integer. You will see that it is under the Connect4View. Its really frustrating as I'm struggling to find a way round this. Is there a possible solution to this and can someone please tell me where I have gone wrong.

Thank you

Connect4Model

public class Connect4Model
{
    Connect4Column [] columns; // an array of columns
    int NUM_COLUMNS; // how many columns we have in the game
    int NUM_ROWS;    // how many rows we have in the game
    int playerToGoNext = Connect4Column.RED_COUNTER; //can be red or yellow
    int autoplay;

    public Connect4Model(int numCols, int numRows)
    {
        NUM_COLUMNS = numCols;
        NUM_ROWS = numRows;

        columns = new Connect4Column[NUM_COLUMNS];
        for (int i=0; i < NUM_COLUMNS; i++) {
            columns[i] = new Connect4Column(NUM_ROWS);
        }
    }

    int getNumCols() // simply return the number of columns
    {
        return NUM_COLUMNS;
    }

    int getNumRows() // simply return the number of rows
    {
        return NUM_ROWS;
    }

    int getNextPlayer() // returns who gets to go next
    {
        return playerToGoNext;
    }
    void reset()
    {
        for (int thisCol = 0; thisCol < NUM_COLUMNS; thisCol ++)
        {
            for (int thisRow = 0; thisRow < NUM_ROWS; thisRow ++)
            {
                columns[thisCol].numCounters = 0;
                columns[thisCol].counters[thisRow] = 0;
            }
        }
    }
    int getAutoplay()
    {
        return autoplay;
    }
    boolean setAutoplay(int selectedOption)
    {
        if(selectedOption >= 1 && selectedOption <= 3) // valid
        {
            autoplay = selectedOption;
            return true;

        }
        else
            return false;
    }

    int getSuggestedMove(int player_colour)
    {
        java.util.Random r = new java.util.Random();
        return r.nextInt(NUM_COLUMNS);
    }

    boolean go(int thisColumn) // try to put a counter at this col
    {
        if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
            return false;

        //System.out.println("thisCol is " + thisColumn);
        if(columns[thisColumn].addCounter(playerToGoNext)==true)
        {
            if(playerToGoNext==Connect4Column.YELLOW_COUNTER)
                playerToGoNext=Connect4Column.RED_COUNTER;
            else
                playerToGoNext=Connect4Column.YELLOW_COUNTER;
            return true;
        }
        return false;
    }

    int getCounter(int thisColumn, int thisRow) //
    {
        if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
            return 0;
        return columns[thisColumn].getCounter(thisRow);
    }
    int getNumCounters(int thisColumn) //
    {
        if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
            return 0;
        return columns[thisColumn].getNumCounters();
    }

    public void(firstGo)
    {
        if(autoplay == playerToGoNext)
        {
            try { Thread.sleep(1000);} catch (Exception e){};.

        }
            else





}

Connect4View

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class Connect4View extends JFrame
{
    DrawPanel canvas;
    Connect4Model model;
    ControlPanel myControlPanel, JRadioButton;


    public static void main(String[] args)
    {
        Connect4View w = new Connect4View();
        w.setVisible(true);
    }

    public Connect4View()
    {
        setTitle("Connect4 solution");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,220);
        setLocation(300,300);

        int number_of_columns = 7;
        int number_of_rows = 9;

        model = new Connect4Model(number_of_columns,number_of_rows);
        canvas = new DrawPanel(number_of_columns,number_of_rows);
        myControlPanel = new ControlPanel();


        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        add(canvas, BorderLayout.CENTER);
        add(myControlPanel, BorderLayout.SOUTH);
    }
    class ControlPanel extends JPanel implements ActionListener
    {
        JButton resetButton;
        JRadioButton twoPlayerButton;
        JRadioButton autoRedButton;
        JRadioButton autoYellowButton;
        JTextField numRows;
        JTextField numCols;

        public ControlPanel()
        {

            resetButton = new JButton("Reset Board");
            add(resetButton);
            resetButton.addActionListener(this);

            twoPlayerButton = new JRadioButton("Two Player Game");
            twoPlayerButton.addActionListener(this);
            add(twoPlayerButton);
            twoPlayerButton.setSelected(true);

            autoRedButton = new JRadioButton("Red Button");
            autoRedButton.addActionListener(this);
            add(autoRedButton);

            autoYellowButton = new JRadioButton("Yellow Button");
            autoYellowButton.addActionListener(this);
            add(autoYellowButton);

            ButtonGroup group = new ButtonGroup();
            group.add(twoPlayerButton);

            ButtonGroup group2 = new ButtonGroup();
            group.add(autoRedButton);

            ButtonGroup group3 = new ButtonGroup();
            group.add(autoYellowButton);

            numRows = new JTextField(4);
            add(numRows);
            numRows.addActionListener(this);
            numRows.setText("Rows");
            int newCol = Integer.parseInt(numRows.getText());


            numCols = new JTextField(4);
            add(numCols);
            numCols.addActionListener(this);
            numCols.setText("Cols");
            int newRow = Integer.parseInt(numCols.getText());


        }

        public void actionPerformed(ActionEvent event)
        {

            if(event.getSource()==twoPlayerButton)
            {
                System.out.println("Selected Two Player");
                model.setAutoplay(1);
                System.out.println("Two Player game");
            }
            else if(event.getSource()==autoRedButton)
            {
                System.out.println("Selected Red Button");
                model.setAutoplay(2);
                System.out.println("Computer Playing Yellow");
            }
            else if(event.getSource()==autoYellowButton)
            {
                System.out.println("Selected Yellow Button");
                model.setAutoplay(3);
                System.out.println("Computer Playing Red ");
            }
            else if(event.getSource()==resetButton)
            {

                model.reset();
                System.out.println("Colum is " + numCols.getText());
                System.out.println("Rows is " + numRows.getText());
            }
            canvas.repaint();
        }
    }
    class DrawPanel extends JPanel implements MouseListener
    {
        public int numCols;
        public int numRows;

        public DrawPanel(int nc, int nr)
        {
            numCols = nc;
            numRows = nr;
            addMouseListener(this);
        }
        int getCol(int x)
        {
            return x*numCols/getWidth();
        }
        int getRow(int y)
        {
            return y*numRows/getHeight();
        }

        public void mouseReleased(MouseEvent event)
        {
        }
        public void mousePressed(MouseEvent event)
        {
        }
        public void mouseClicked(MouseEvent event)
        {
            int thisCol = getCol(event.getX());
            model.go(thisCol);

            if(model.autoplay == 1)
            {
                System.out.println("Two Player game");
            }
            else if(model.autoplay == 2)
            {

                thisCol = model.getSuggestedMove(model.getNextPlayer());
                model.go(thisCol);
            }
            else if(model.autoplay == 3)
            {
                thisCol = model.getSuggestedMove(model.getNextPlayer());
                model.go(thisCol);
            }
            //model.firstGo();
            repaint();

        }
        public void mouseEntered(MouseEvent event)
        {
        }
        public void mouseExited(MouseEvent event)
        {
        }
        Rectangle getRect(int thisCol, int thisRow)
        {
            // if input is out of range, return "null"
            if(thisCol <0 || thisRow < 0)
                return null;
            if(thisCol>=numCols || thisRow>=numRows)
                return null;

            // otherwise, make and return the Rectangle
            int w = getWidth()/numCols;
            int h = getHeight()/numRows;

            int x = thisCol*w;
            int y = thisRow*h;

            Rectangle myRect = new Rectangle(x,y,w,h);
            return myRect;
        }

        public void paint(Graphics g)
        {
            g.setColor(Color.gray);
            g.fillRect(0,0,getWidth(), getHeight());
            g.setColor(Color.black);


            Graphics2D g2 = (Graphics2D)g;
            // we'll use Graphics2D for it's "draw" method -
            // neater than the Graphics "drawRect" suppled
            // (which you could also use)

            for (int i = 0;i<numCols;i++)
                for(int j = 0;j<numRows;j++)
                    g2.draw(getRect(i,j));

            for (int thisCol = 0; thisCol < model.getNumCols(); thisCol ++)
            {
                int num_of_counters = model.getNumCounters(thisCol);
                //System.out.println("col " + thisCol + " has " + num_of_counters);
                for (int counter=0; counter < num_of_counters; counter ++)
                {
                    int colour = model.getCounter(thisCol, counter);
                    if(colour==Connect4Column.RED_COUNTER)
                        g2.setColor(Color.red);
                    else if(colour==Connect4Column.YELLOW_COUNTER)
                        g2.setColor(Color.yellow);

                    Rectangle r = getRect(thisCol, numRows-counter-1);
                    if (r != null)
                        g2.fillOval(r.x, r.y, r.width, r.height);
                }
            }
        }
    }

 }

Connect4Column

public class Connect4Column
{
    int [] counters; // array to store counters in this column
    int numCounters; // integer to say how many counters currently in this column
    int MAX_NUM_COUNTERS;  // height of the 'Connect 4' game

    static int YELLOW_COUNTER = 1;  // represents the 'yellow' counters
    static int RED_COUNTER = 2;  // represents the 'red' counters

    public Connect4Column(int maxCounters)
    {
        MAX_NUM_COUNTERS = maxCounters;
        counters = new int[MAX_NUM_COUNTERS];
        numCounters = 0;
    }

    public boolean addCounter(int thisCounter)
    {
        if (numCounters>=MAX_NUM_COUNTERS)
            return false;

        if (thisCounter == YELLOW_COUNTER
             || thisCounter == RED_COUNTER)
        {
            counters[numCounters] = thisCounter;
            numCounters ++;
            return true;
        }
        else
            return false;
    }

    int getNumCounters()
    {
        return numCounters;
    }

    int getCounter(int thisRow)
    {
        if(thisRow<0 || thisRow >= MAX_NUM_COUNTERS)
            return 0;

        return counters[thisRow];

    }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Not going to read all that code to try and figure out what "runtime errors" you're getting.
Post the exact errors, why you think they're happening and where, and maybe we'll tell you if you're right.

Not going to read all that code to try and figure out what "runtime errors" you're getting.
Post the exact errors, why you think they're happening and where, and maybe we'll tell you if you're right.

This is the error I'm getting.

Exception in thread "main" java.lang.NumberFormatException: For input string: "Rows"

Not going to read all that code to try and figure out what "runtime errors" you're getting.
Post the exact errors, why you think they're happening and where, and maybe we'll tell you if you're right.

This is the error I'm getting.

Exception in thread "main" java.lang.NumberFormatException: For input string: "Rows"
at java.lang.NumberFormatexceptio.forInputString<NumberFormatexception.java:48

at java.lang.Integer.parseInt<Integer.java:449>
at java.lang.Integer.parseInt<Integer.java:499>
at Connect4View$ControlPannel.<init><Connect4View.java.85>
at Connect4View.<init><Connect4View.java:35>
at Connect4View.main<Connect4View.java:19

Yes you are going to get that error. You are not able to convert Rows to a number.

I've managed to solve the reason why I was unable to convert the Rows into a number. however what now trying to do is move Connect4Model constructor into the void reset method. And what it should do is take the two parameters columns and rows. I got the reset() method to look like something like this

void reset(int numCols, int numRows)
	{

			NUM_COLUMNS = numCols;
			NUM_ROWS = numRows;
			columns = new Connect4Column[NUM_COLUMNS];
			for (int i=0; i < NUM_COLUMNS; i++) {
			columns[i] = new Connect4Column(NUM_ROWS);


		for (int thisCol = 0; thisCol < NUM_COLUMNS; thisCol ++)
		{
			for (int thisRow = 0; thisRow < NUM_ROWS; thisRow ++)
			{
				columns[thisCol].numCounters = 0;
				columns[thisCol].counters[thisRow] = 0;
			}
		}
	}

You can see above in the previous post what i have more or less done. Is there a possible way around this.

This question has already been answered. Start a new discussion instead.