+ 1

PLEASE HELP WITH JAVA

Java max length 2-D array help PLEASE Please help out over here for java So I’m doing an assignment where we need to return the number of characters in the table that has the largest number of characters. For example: Int [] [] table = {{-3, 18, -2053, 13}; {1856, 24601, 7, 92}, {23, 0, 425, -25}}; Return: 5 because -2053 has 5 characters. I’m just having trouble figuring out how to make the three tables plus for it to read the largest amount of characters. PLEASE HELP I was able to make a code which will read the largest number. public class MaxLength { static int arr[] = { 3, 18, 10,13}; static int largest() { int i; int max = arr[0]; for (i = 1; i < arr.length; i++) if (arr[i] > max) max = arr[i]; return max; } public static void main(String[] args) { System.out.println("Largest in given array is " + largest()); } } All I’m asking if someone can help me out with this code. I really really really need help and I will appreciate any help with this code. This is the assignment: So I’m doing an assignment where we need to return the number of characters in the table that has the largest number of characters. For example: Int [] [] table = {{-3, 18, -2053, 13}; {1856, 24601, 7, 92}, {23, 0, 425, -25}}; Return: 5 because -2053 has 5 characters

23rd Nov 2020, 6:49 AM
Hello
Hello - avatar
7 Answers
23rd Nov 2020, 7:15 AM
Avinesh
Avinesh - avatar
+ 4
This one takes additional steps of finding highest positive and negative value. The additional steps is performed in order to reduce number -> string conversion frequency. https://code.sololearn.com/cMFukK8qB02Y/?ref=app
23rd Nov 2020, 8:01 AM
Ipang
+ 3
Hello You need to check for digits count not for value then you can convert any number to string (check for String.valueOf method) and get the size from that, then apply the comparison. Anyway, just like said by Ipang , i dont understand if you need to return a number in particular or not
23rd Nov 2020, 7:01 AM
KrOW
KrOW - avatar
+ 3
- for any number do - transform the number to string - get the string length - compare with current max length - if its more than current max length, update it to current number length
23rd Nov 2020, 7:08 AM
KrOW
KrOW - avatar
+ 2
A slightly cleaner modification of Avinesh code. int maxCount = 0; int n = 0; for(int[] arr : table) { for(Integer num : arr) { n = num.toString().length(); if(n > maxCount) { maxCount = n; } } }
23rd Nov 2020, 7:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
i see
23rd Nov 2020, 7:02 AM
Hello
Hello - avatar
+ 1
how would the code format go? I started taking java seriously, but im still in the process of learning. So, im having problems understanding the code.
23rd Nov 2020, 7:05 AM
Hello
Hello - avatar