簡體   English   中英

通過使用循環訪問每個索引將字符串數組轉換為整數數組

[英]Converting String Array into Integer Array by accessing each indexes using loop

我在下面有我的示例代碼。 我正在嘗試將gradeString 轉換為gradeInt,但似乎無法正確執行。 當我在最后使用 toString 打印 GradeInt 的值時。

它說一個像這樣的空白數組:[]

謝謝您的幫助!

import java.util.*;

class testing{
    public static Scanner console = new Scanner(System.in);
    public static int studSize;
    public static String [] gradeArr = new String[studSize];
    public static int [] gradeInt = new int[gradeArr.length];

    public static void initialize(){
        System.out.print("Please enter student size: ");
        String studString = console.nextLine();
        studSize = Integer.parseInt(studString);
        enterData();
    }
    public static void enterData(){
        System.out.println("Enter student grades separated with dash(-)");
        System.out.print("Enter student grade/s: ");
        String gradeString = console.nextLine();
        gradeArr = gradeString.split("-");
        convert();
    }
    public static void convert(){
        for(int i=0; i<gradeInt.length; i++){
            gradeInt[i] = Integer.parseInt(gradeArr[i]);
        }
        print();
    }
    public static void print(){
        System.out.print(Arrays.toString(gradeArr));
        System.out.print(Arrays.toString(gradeInt));
    }
    public static void main(String [] args){
        initialize();
        //Main and Class Closing Braces
    }
}

在您調用convert為“快速修復”之前添加此行:

gradeInt = new int[gradeArr.length];

嘗試這個。 問題是您在輸入任何數據之前分配數組的大小等。 實際上根本不需要使用studSize - 請參閱代碼中的注釋

public static Scanner console = new Scanner(System.in);
public static int studSize;
public static String [] gradeArr;
public static int [] gradeInt;

public static void initialize(){

    // not needed - 
    System.out.print("Please enter student size: ");
    String studString = console.nextLine();

    // not needed 
    studSize = Integer.parseInt(studString);
    enterData();
}
public static void enterData(){
    System.out.println("Enter student grades separated with dash(-)");
    System.out.print("Enter student grade/s: ");
    String gradeString = console.nextLine();
    gradeArr = gradeString.split("-");
    convert();
}
public static void convert(){
    // set here as the size is know
    gradeInt = new int[gradeArr.length];

    for(int i=0; i<gradeArr.length; i++){
        // should be carefull of NumberFormatException
        gradeInt[i] = Integer.parseInt(gradeArr[i]);
    }
    print();
}
public static void print(){
    System.out.print(Arrays.toString(gradeInt));
}
public static void main(String [] args){
    initialize();
    //Main and Class Closing Braces
}

我運行了你的代碼並且它有效

輸出:

[95, 85, 75]
Process finished with exit code 0

如果你得到一個空數組,我懷疑你的代碼的其他部分可能有錯誤。

您運行代碼的上下文是什么? 如果您粘貼其余代碼,它可能有助於找到錯誤的真正來源。

解決方案:

就像安迪提到的那樣。 根錯誤包含在這里:

public static int [] gradeInt = new int[gradeArr.length];

您在運行時定義此變量並為其分配與當時為 0 的 gradeArr 相同的長度。

這會導致在 convert() 方法中發生邏輯錯誤。

    for(int i=0; i<gradeInt.length; i++){
        gradeInt[i] = Integer.parseInt(gradeArr[i]);
    }

如果 i 小於具有 0 長度的gradeInt,您告訴代碼循環。基本上它從不循環。 它只是完全跳過循環。

解決這個問題的最快方法是像這樣修改代碼:

不要在運行時為 gradeInt 變量分配長度。 將這一行修改為:

public static int [] gradeInt;

然后將您的 enterData() 方法修改為:

public static void enterData(){
    System.out.println("Enter student grades separated with dash(-)");
    System.out.print("Enter student grade/s: ");
    String gradeString = console.nextLine();
    gradeArr = gradeString.split("-");

    gradeInt = new int[gradeArr.length];
    convert();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM