0

I'm really new to java or programming, so i have a "basic" quesion (I guess). I have this code, which generates me an array of numbers. I want to convert that into an array of strings. Can someone please fix my code?

public class HelloWorld{
    public static void main(String[] args) {
        Number[][] values = new Number[10][12];
        for (int i = 0; i < values.length; i++) {
            for (int j = 0; j < values[i].length; j++) {
                values[i][j] = Math.floor(Math.random() * 100000) / 100000;

                System.out.print(values[i][j] + " ; ");
            }
            System.out.println();
        }
    }
}
2
  • Do you still need the array of Numbers afterwards? If not, just convert the number to a string as soon as you generate it an declare values as String[][]. As an aside, why use Number when double is more than adequate? Commented Oct 14, 2020 at 12:10
  • Yes, I need the numbers afterwards. And with the doubles / numbers array, thank you for that hint Commented Oct 14, 2020 at 12:14

2 Answers 2

2

You can simply use String array instead of Number array. When setting the element, use String.valueOf(YOUR_NUMBER)

Sign up to request clarification or add additional context in comments.

2 Comments

So I write String [][] values = new String[12][10]; and than? Where do I use your String.valueOf?
In here just add: `values[i][j] = String.valueOf(Math.floor(Math.random() * 100000) / 100000);
1

The most obvious way:

public static String[][] convert(Number[][] numbers) {
        String[][] result = new String[numbers.length][];
        for (int i = 0; i < numbers.length; i ++) {
            Number[] row = numbers[i];
            result[i] = new String[row.length];
            for(int j = 0; j < row.length; j ++) {
                result[i][j] = row[j] == null ? null : row[j].toString();
            }
        }
        return result;
    }

Less obvious way:

public static String[][] convert(Number[][] numbers) {
        return Arrays.stream(numbers)
                .map(row -> Arrays.stream(row).map(n -> String.valueOf(n)).toArray(String[]::new))
                .toArray(String[][]::new);
    }

6 Comments

Thank you, but how do I add this to my class? Just as a second method or do I have to change something?
Yes, add method to your class and call it like String [][] strValues = convert(values);
Do I have to rename it? Cause when I copy & paste it and than change the line to "String [][] strValues = convert(values);", I get some error messages. I'm really new to java so sorry for that stupid quesions.
No need to rename, it should be enough to place method inside your class and make sure it's static when calling it from main().
Okay thank you, but one last question: how are these two methods connected?
|

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.