Please help out over here for java | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

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

21st Nov 2020, 6:09 AM
Hello
Hello - avatar
2 Antworten
+ 1
your code returns 18 instead -2053 you have to get the length of the number . convert num to String by String.valueOf(num) . compare the length of strings len > maxLen . but store and return num not length for 2D array use two loops for(int i ..) for(int j ..) .. max = array[i][j];
21st Nov 2020, 1:56 PM
zemiak
+ 1
With that approach you will get max value not max length value because you have Negetive value -2053 > 18 false so you need to find length of numbers.. Or use absolute method for comparing.. Like if (Math.abs(arr[i]) >= Math.abs(max)) Now you will get -2053 as largest. But this fails when you have 2053 1st and next -2053 here you get 2053 not -2053 so find length of number.. Like 2053/10=>/10=>/10=>/10 =0 so length is 4. for Negetive numbers add 1, length=length+1 Hope it helps..
21st Nov 2020, 2:00 PM
Jayakrishna 🇮🇳