0

I have my example code below. I'm trying to convert the gradeString into gradeInt and I can't seem to do it properly. When I print the value of gradeInt at the end using toString.

It says a blank array which is like this one: [ ]

Thanks for the help!

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
    }
}
3
  • As per @SideraMaris, this code works Commented Feb 12, 2020 at 2:18
  • I used methods for the overall context of the program. Please see my update post, thank you! Commented Feb 12, 2020 at 2:29
  • 1
    gradeInt is allocated based on the initial size of gradeArr which is 0. you'll need to allocate gradeInt after getting the size - which is a result of the split - so use gradeArr.lengthto size it - e.g. just before calling convert. Commented Feb 12, 2020 at 2:36

3 Answers 3

1

Add this line just before your call to convert for the "quick fix":

gradeInt = new int[gradeArr.length];
Sign up to request clarification or add additional context in comments.

3 Comments

Like the quick_fix comment
Thanks! So, I should re-declare gradeInt as a new object so that it'll continue.
No redeclare - you can however remove the static initialization and just initialize once as stated in answer. Similar with gradeArr - it is allocated by the "split" method.
0

Try this. The problem is that you are assigning sizes of arrays etc before any data is entered. Actually the usuage of studSize is not needed at all - see comments in code

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
}

3 Comments

I'm wondering, shouldn't the TO get an exception if the split fails and the whole string will be parsed as integer?
Ohhh. You're right! Thanks for the idea and answer.
@UninformedUser. It does error, still trying to learn Java thoo. Anyways, thanks!
0

I ran your code and it works

Output:

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

I suspect there may be an error in other parts of your code if you're getting a empty array.

What's the context you're running the code in? If you paste the rest of the code it might help finding the real source of the error.

SOLUTION:

Like Andy mentioned. The root error is contained here:

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

You're defining this variable at runtime and assigning it the same length as gradeArr which is 0 at the time.

This results in a logic error that happens in the convert() method.

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

You are telling the code to loop if i was smaller than gradeInt which has a length of 0. Basically it never loops. It just skips the loop entirely.

The quickest way to fix this is to modify the code like this:

Don't assign a length to the gradeInt variable at runtime. Modify the line to this:

public static int [] gradeInt;

And then modify your enterData() method to this:

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();
}

2 Comments

I used methods for the overall context of the program. Please see my update post, thank you!
Hey James I've found the error and posted the quickest fix. EDIT: Looks like Wombat beat me to it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.