且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java的int数组返回全0

更新时间:2023-12-03 14:33:22

在初始化后的int数组默认为全零 - 这似乎是你的情况。无处你设置int数组中的任何值,你只初始化,然后阅读它。

Hello all and thanks for taking the time to look at my question. I'm working on my Java homework (I understand the rules and I don't want you to do my homework, I'm just very stuck and am very confused so please to ban/yell at/poke me)

I have a class called Encryption. I'm calling this class in a Panel, which is being put into a Frame.

I need to read in user input and 'encrypt' that string with my own system using an Array.

I have read my book and searched for answers but I don't know why my INT array is returnign all O's. My Char array is returning the correct Char when I debug it, but my Int array is returning all 0's.

Here is what I have, any advice or suggestions is much appreciated.

Thanks

 import java.util.Scanner;

 public class Encryption {

private String finalEncryption;
int [] numArray = new int[25];
char[] charArray = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char current;

//constructor
public Encryption(){

}

public String toString(){

    return finalEncryption;
}

public String setEncryption(String entry){

    String newEntry = entry.toUpperCase();

    //loop to go through each letter in the string
    for (int ch = 0; ch < newEntry.length(); ch++)
    {
        current = newEntry.charAt(ch);

        //loop to go through each letter in the alphabet
        for (int i=0; i < 26; i++)
        {
            if(current == charArray[i])
            {
                int finalEntry = numArray[i];
                System.out.println(finalEntry);

            }
            else if (current == numArray[i])
            {

            }

        }

        System.out.println(current);
    }

    return entry;
}

 }

An int array defaults to all zero's after you initialize it - this appears to be your case. Nowhere are you setting any values in the int array, you are only initializing it, and then reading it.