Home > Enterprise >  String to integer list or integer array [duplicate]
String to integer list or integer array [duplicate]

Time:09-23

Hi I'm new to java I want to change a String "3,4,5" to an Array of integer like this "[3,4,5]"

String dayOfWeekShipment = "3,4,5";

String[] dayOfWeekShipmentArString = dayOfWeekShipment.split(",");
        
log.info(String.valueOf(dayOfWeekShipmentArString));
        
int[] dayOfWeekShipmentArInt = new int[dayOfWeekShipmentArString.length];
for (int i = 0; i < dayOfWeekShipmentArString.length; i  ) {
    dayOfWeekShipmentArInt[i] = Integer.parseInt(dayOfWeekShipmentArString[i]);
}

I need to loop it later to find the closest number to Integer todayDay for example

Integer tempInt = 7;
Integer todayDay = 1;
for (int i = 0; i < dayOfWeekShipment.length(); i  ) {
   if(dayOfWeekShipmentArInt[i] > todayDay && dayOfWeekShipmentArInt[i] <= tempInt){
      tempInt = dayOfWeekShipmentArInt[i];
   }
 }

It supposed to work but , it shows error like this Ljava.lang.String;@6f57e7dd

Some said I should use List and change it to List

Any solution so I can loop that string 3,4,5 to Integer and put my logic to it ?

I tried using Log info on

String[] dayOfWeekShipmentArString = dayOfWeekShipment.split(",");
        log.info(String.valueOf(dayOfWeekShipmentArString));
        int[] dayOfWeekShipmentArInt = new int[dayOfWeekShipmentArString.length];
        for (int i = 0; i < dayOfWeekShipmentArString.length; i  ) {
            dayOfWeekShipmentArInt[i] = Integer.parseInt(dayOfWeekShipmentArString[i]);
            }
            log.info(String.format("dayOfWeekShipmentArInt = '%s'",dayOfWeekShipmentArInt ));

I'm expecting integers on both result but It shows message : Ljava.lang.String;@6f57e7dd from log.info

CodePudding user response:

It supposed to work but , it shows error like this Ljava.lang.String;@6f57e7dd

That's simply because you're not printing it correctly.

You can't print Java arrays directly. You have to use Arrays.toString:

log.info(Arrays.toString(dayOfWeekShipmentArString));

An shorter way to convert to an int[] could be:

int[] dayOfWeekShipmentArInt =
    Arrays.stream(dayOfWeekShipment.split(","))
        .mapToInt(Integer::parseInt)
        .toArray();

which you can print in the same way:

log.info(Arrays.toString(dayOfWeekShipmentArInt));

CodePudding user response:

Ljava.lang.String;@6f57e7dd is not error, while it's info message. There is some other length related issue in code.

In your code for second loop to find closest number you are iterating from i =0 to the length of String which is 5 (length - 1 = 4). While here you need to iterate from i = 0 to length of numbers in string which is 4 (length -1 = 2).

So your code should be like this in second loop :

for (int i = 0; i < dayOfWeekShipmentArInt.length; i  ) {
       if(dayOfWeekShipmentArInt[i] > todayDay && dayOfWeekShipmentArInt[i] <= tempInt){
          tempInt = dayOfWeekShipmentArInt[i];
       }
     }

And definitely you can improve your code with streams, while currently in going in that direction.

  • Related