简体   繁体   中英

Why won't my array satisfy the conditions of this if-statement?

I'm doing this program for an assignment and I have absolutely zero idea why my array (reading values from a separate file) won't satisfy the conditions of this if-statement. For your convenience, the statement in question is located in the displayColonies class.

When i read in my array, it looks for the value at a specified row and column, which is an int value registering from 1-9 inclusive. When I tested this periodically using print statements, the array does contain the correct value, but the if-statement is never activated. The conditions match with the array's value, but the statement is never evaluated, true or otherwise.

Thanks for your help.

Code is attached below: DetectColonies2ElectricBoogaloo is client Slide is Object slide.dat is text file, arranged as follows

6
8
10550000
00050000
00005500
01200000
01111000
00000030
public class DetectColonies2ElectricBoogaloo {

    public static void main(String [] args) {

        Slide culture = new Slide("Slide.dat");
        culture.displaySlide();
        culture.displayColonies();
   }
}
import java.io.*;

public class Slide {

    private char NON_COLONY = '0';
    private char [][] slideData;

    /**
     * constructor
     * pre: Slide file contains valid slide data in the format:
     * first line: lenght of slide
     * second line: width of slide
     * remaining lines: slide data
     * post: Slide data has been loaded from slide file.
     */
    public Slide(String s) {    
        try {
            File slideFile = new File(s);
            FileReader in = new FileReader(slideFile);
            BufferedReader readSlide = new BufferedReader(in);

            int length = Integer.parseInt(readSlide.readLine());
            int width = Integer.parseInt(readSlide.readLine());
            slideData = new char[length][width];

            for (int row = 0; row < length; row++) {
                for (int col = 0; col < width; col++) {
                    slideData[row][col] = (char)readSlide.read();
                }
                readSlide.readLine();   //read past end-of-line characters
            }
            readSlide.close();
            in.close();
       } catch (FileNotFoundException e) {
            System.out.println("File does not exist or could not be found.");
            System.err.println("FileNotFoundException: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Problem reading file.");
            System.err.println("IOException: " + e.getMessage());
        }
    }

    /**
     * Determines a colony size
     * pre: none
     * post: All colony cells adjoining and including cell (Row, Col) have 
     * been changed to NON_COLONY, and count of these cells is returned.
     */
    private int collectCells(int row, int col , char colour) {

        if ((row < 0) || (row >= slideData.length) || (col < 0) || (col >= slideData[0].length) || (slideData[row][col] != colour)) {
            return(0);
        } else {
            slideData[row][col] = NON_COLONY;
            return(1 + 
                collectCells(row + 1, col , colour) + 
                collectCells(row - 1, col , colour) + 
                collectCells(row, col + 1 , colour) + 
                collectCells(row, col - 1 , colour) + 
                collectCells(row - 1 , col - 1 , colour) + 
                collectCells(row + 1 , col + 1 , colour) + 
                collectCells(row - 1 , col + 1 , colour) + 
                collectCells(row + 1 , col - 1 , colour));
        }
    }

    /**
     * Analyzes a slide for colonies and displays colony data
     * pre: none
     * post: Colony data has been displayed.
     */
    public void displayColonies() {
        int count;
        System.out.format("%-10s %-10s %-10s" , "LOCATION" , "SIZE" , "COLOUR");
        System.out.println();

        for (int row = 0; row < slideData.length; row++) {
            for (int col = 0; col < slideData[0].length; col++) {
                for (int i = 1; i <= 9; i++) {
                    if (slideData[row][col] == i) {
                        count = collectCells(row , col , (char)i);
                        System.out.format("%-10s %-10s %-10s" , "(" + row + "," + col + ")" , count , i);
                    }
                }
            }
        }
    }


    /**
     * Displays a slide.
     * pre: none
     * post: Slide data has been displayed.
     */
    public void displaySlide() {

        for (int row = 0; row < slideData.length; row++) {
            for (int col = 0; col < slideData[0].length; col++) {
                System.out.print(slideData[row][col]);
            }
            System.out.println();
        }
    }
}

You are comparing an int (the value in i ) (eg 3) against characters (like '1', '2'...) (the values in slideData ) which as integers are values starting at 0x30 ('0')..

So without rewriting your program the simplest solution to fix your incompatible types by converting your for loop value (1..9) to the character equivalent as in the following:

for (int i = 1; i <= 9; i++)
{
    char x = (char) (0x30 + i);
    if (slideData[row][col] == x)
    {
       // ...
    }
}

To convert the integer (digit) to a char you could also do:

  char x = Character.forDigit(i, 10);

Change

col >= slideData[0].length

to

col >= slideData[row].length

inside method private int collectCells(int row, int col, char colour)

As others also pointed out - you are comparing a char (character) to an int (integer). I can be a little confusing when you see that the value in your array slideData at cell [0][0] (row=0 and col=0) contains 1, the value in i is also 1 but the condition fails. But there's a difference between what is stored in your array of characters and the value of an integer variable.

In Java when you compare a character to an integer the JVM uses the character's ASCII value. it means the value 1,= '1'. since the ASCII value of '1' is 49.

it's possible you don't see the difference because of how your IDE's debugger shows the values of your character array. In IntelliJ, for example, you will see: ![slideData[0][0] value and ASCII value] 1 It shows you that the value in slideData[0][0] is '1' (character value) and its ASCII (integer) value is 49. Your condition will work if you make a small change to it:

if (slideData[row][col] == 48 + i) {
   count = collectCells(row , col , (char)i);
   System.out.format("%-10s %-10s %-10s" , "(" + row + "," + col + ")" , count , i);
}

The integer value 48 is the ASCII value of '0' character, adding the value of i to it will give you the ASCII value of the character you are comparing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM