Results 1 to 7 of 7

Thread: Type Mismatch

  1. #1
    user23 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Type Mismatch

    I'm trying to make a puzzle that gets the user input and moves the rows either to the left or right, the columns move either up or down depending on what the user wants. The problem I'm getting is a type mismatch for my RL method which moves the rows to the left.


    Java Code:
    import java.util.*;  
    import java.io.*;  
      
    public class Numbrosia {  
        static int [][] board = new int [5][5];  
        public static void main(String[]args){  
            Scanner scan = null;  
            try{  
                scan = new Scanner(new File("input.txt"));  
            }  
            catch(FileNotFoundException e){  
                e.printStackTrace();  
                return;   
            }  
            for(int row = 0; row<board.length; row++){  
                for(int col= 0; col<board.length;col++){  
                    board[row][col] = scan.nextInt();  
                }  
            }  
            while(true){  
                showBoard();  
                Scanner kbScan= new Scanner(System.in);  
                System.out.println("");  
                System.out.println("Input number from 1 to 5: ");  
                int  i = kbScan.nextInt();  
                System.out.println("Input move command: ");  
                String moveName = kbScan.next();  
                //If/ else statements to dictate which method to call  
                if(moveName.equals("rl")){  
      
                    RL(i-1);  
                }  
                else if(moveName.equals("rr")){  
      
                    RR(i+1);  
                }  
                else if(moveName.equals("ru")){  
      
                    RU(i-1);  
                }  
                else if(moveName.equals("rd")){  
      
                    RD(i-1);  
                }  
                else if(moveName.equals("+r")){  
      
                    plusRow(i-1);  
                }  
                else if(moveName.equals("-r")){  
      
                    minusRow(i-1);  
                }  
                else if(moveName.equals("+c")){  
      
                    plusColumn(i-1);  
                }  
                else if(moveName.equals("-c")){  
      
                    minusColumn(i-1);  
                }  
                else{  
                    System.out.println("");  
                    System.out.println("Please follow instructions");  
                    System.out.println("");  
                }  
            }  
            // In case user inputs # greater than 5  
      
      
        }  
        public static void showBoard(){  
            for(int row = 0; row < board.length; row++){  
                for(int col = 0; col<board.length;col++){  
                    System.out.print(board[row][col] + " ");  
                    System.out.print(" ");  
                }  
                System.out.println(" ");  
            }  
        }  
      
        public static void RL(int userPosition, int[][] board){  
            for(int row = 0; row< board.length ;row++){  
                for(int col = 0; col<board.length;col++){  
                    if (board.length > 0) {  
                        int first = board[0][0];  
                          
      
                        for (int i = 0; i < board.length-1; i++) {  
                      
                          board[i] = board[i+1];  
                            System.out.print(board[i]);  
                        }  
      
                       board[board.length-1] = first;  
                        System.out.print(board[board.length-1]);  
                        }  
      
                    System.out.println("After:");  
                    for (int i = 0; i < board.length; i++) {  
                       System.out.print(board[i] + ", ");  
                    }  
            }  
            }
    output I'm getting:
    1 -2 1 0 0
    -1 0 4 2 0
    0 -4 1 -1 0
    0 1 -1 -1 -2
    0 -3 1 -1 0



    Output I want if the user wants to move the first row:
    -2 1 0 0 1
    -1 0 4 2 0
    0 -4 1 -1 0
    0 1 -1 -1 -2
    0 -3 1 -1 0


    Error message:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Type mismatch: cannot convert from int to int[]

    at excercises.Numbrosia.RL(Numbrosia.java:95)
    at excercises.Numbrosia.main(Numbrosia.java:32)
    Last edited by user23; 02-16-2014 at 10:26 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Type Mismatch

    problem I'm getting is a type mismatch
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Is there code missing at the end?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    user23 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Type Mismatch

    I put the error message

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Type Mismatch

    Type mismatch: cannot convert from int to int[]
    at excercises.Numbrosia.RL(Numbrosia.java:95)
    The code on line 95 is trying to convert an int variable to an int array.

    Java Code:
       board[board.length-1] = first;  
     System.out.print(board[board.length-1]);
    board is a 2 dim array. To get at an element the code needs 2 indexes: [2][3]
    board[ix] is a 1 dim array. You can not assign an int where a 1 dim array is required.


    To print the sub array in a 2 dim array(board[ix] is the sub array) use the Arrays class's toString() method to format it for printing.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Type Mismatch

    there is explanation in error message. you are providing RL method with int instead array of int[]

  6. #6
    user23 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Type Mismatch

    If I use the toString() I should be able to print the array. Should I do something else to print my 2d array so I can see the change in my grid.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Type Mismatch

    to print my 2d array
    Use the Arrays class's deepToString() method to format it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 08-18-2013, 03:52 AM
  2. Type mismatch (String)
    By TSTONESIX in forum New To Java
    Replies: 2
    Last Post: 03-12-2013, 03:13 AM
  3. Replies: 3
    Last Post: 03-26-2012, 12:34 PM
  4. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 11:16 AM
  5. Replies: 2
    Last Post: 11-16-2007, 01:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •