Java String Array Get Max
I have a 2D String
array and I'd like to print and get the maximum value of the second row. I've searched for methods to get the maximum value of each row but it's not working.
Here's what I have so far:
String stringArray = new String[6][1];
for (int i = 0; i < stringArray.length; i++){
double max = Integer.MIN_VALUE;
System.out.println();
for (int j = 0; j < stringArray[i].length; j++){
System.out.print("Value " + (i + 1) + "t");
stringArray[1][j] = String.valueOf(Math.random());
System.out.print(stringArray[1][j] + "t");
if (Integer.parseInt(stringArray[1][j]) > max){
max = Double.parseDouble(stringArray[1][j]);
}
}
System.out.println(); // to print in matrix format
System.out.println("n Max value is " + max);
}
I tried to change the i
's and j
's and got nothing. The result is so weird (and wrong), probably because it's together with the print matrix format.
Suggestions?
java arrays max
add a comment |
I have a 2D String
array and I'd like to print and get the maximum value of the second row. I've searched for methods to get the maximum value of each row but it's not working.
Here's what I have so far:
String stringArray = new String[6][1];
for (int i = 0; i < stringArray.length; i++){
double max = Integer.MIN_VALUE;
System.out.println();
for (int j = 0; j < stringArray[i].length; j++){
System.out.print("Value " + (i + 1) + "t");
stringArray[1][j] = String.valueOf(Math.random());
System.out.print(stringArray[1][j] + "t");
if (Integer.parseInt(stringArray[1][j]) > max){
max = Double.parseDouble(stringArray[1][j]);
}
}
System.out.println(); // to print in matrix format
System.out.println("n Max value is " + max);
}
I tried to change the i
's and j
's and got nothing. The result is so weird (and wrong), probably because it's together with the print matrix format.
Suggestions?
java arrays max
1
new String[6][1]
- technically it is a 1D array.
– user7
Nov 28 '18 at 18:00
Have you tried using.length
?
– Frontear
Nov 28 '18 at 18:01
Why stringArray[1][j]? Change it tostringArray[i][j]
– forpas
Nov 28 '18 at 18:01
2
Integer.parseInt
will not work here asMath.random()
returns a double. What is your intention here?
– user7
Nov 28 '18 at 18:01
As per this statementString stringArray = new String[6][1];
you will have 6 rows with 1 column -- What do you mean by get the maximum value of the second row when there is just 1 element in each row?
– Nicholas K
Nov 28 '18 at 18:15
add a comment |
I have a 2D String
array and I'd like to print and get the maximum value of the second row. I've searched for methods to get the maximum value of each row but it's not working.
Here's what I have so far:
String stringArray = new String[6][1];
for (int i = 0; i < stringArray.length; i++){
double max = Integer.MIN_VALUE;
System.out.println();
for (int j = 0; j < stringArray[i].length; j++){
System.out.print("Value " + (i + 1) + "t");
stringArray[1][j] = String.valueOf(Math.random());
System.out.print(stringArray[1][j] + "t");
if (Integer.parseInt(stringArray[1][j]) > max){
max = Double.parseDouble(stringArray[1][j]);
}
}
System.out.println(); // to print in matrix format
System.out.println("n Max value is " + max);
}
I tried to change the i
's and j
's and got nothing. The result is so weird (and wrong), probably because it's together with the print matrix format.
Suggestions?
java arrays max
I have a 2D String
array and I'd like to print and get the maximum value of the second row. I've searched for methods to get the maximum value of each row but it's not working.
Here's what I have so far:
String stringArray = new String[6][1];
for (int i = 0; i < stringArray.length; i++){
double max = Integer.MIN_VALUE;
System.out.println();
for (int j = 0; j < stringArray[i].length; j++){
System.out.print("Value " + (i + 1) + "t");
stringArray[1][j] = String.valueOf(Math.random());
System.out.print(stringArray[1][j] + "t");
if (Integer.parseInt(stringArray[1][j]) > max){
max = Double.parseDouble(stringArray[1][j]);
}
}
System.out.println(); // to print in matrix format
System.out.println("n Max value is " + max);
}
I tried to change the i
's and j
's and got nothing. The result is so weird (and wrong), probably because it's together with the print matrix format.
Suggestions?
java arrays max
java arrays max
asked Nov 28 '18 at 17:57
MarceloMarcelo
291
291
1
new String[6][1]
- technically it is a 1D array.
– user7
Nov 28 '18 at 18:00
Have you tried using.length
?
– Frontear
Nov 28 '18 at 18:01
Why stringArray[1][j]? Change it tostringArray[i][j]
– forpas
Nov 28 '18 at 18:01
2
Integer.parseInt
will not work here asMath.random()
returns a double. What is your intention here?
– user7
Nov 28 '18 at 18:01
As per this statementString stringArray = new String[6][1];
you will have 6 rows with 1 column -- What do you mean by get the maximum value of the second row when there is just 1 element in each row?
– Nicholas K
Nov 28 '18 at 18:15
add a comment |
1
new String[6][1]
- technically it is a 1D array.
– user7
Nov 28 '18 at 18:00
Have you tried using.length
?
– Frontear
Nov 28 '18 at 18:01
Why stringArray[1][j]? Change it tostringArray[i][j]
– forpas
Nov 28 '18 at 18:01
2
Integer.parseInt
will not work here asMath.random()
returns a double. What is your intention here?
– user7
Nov 28 '18 at 18:01
As per this statementString stringArray = new String[6][1];
you will have 6 rows with 1 column -- What do you mean by get the maximum value of the second row when there is just 1 element in each row?
– Nicholas K
Nov 28 '18 at 18:15
1
1
new String[6][1]
- technically it is a 1D array.– user7
Nov 28 '18 at 18:00
new String[6][1]
- technically it is a 1D array.– user7
Nov 28 '18 at 18:00
Have you tried using
.length
?– Frontear
Nov 28 '18 at 18:01
Have you tried using
.length
?– Frontear
Nov 28 '18 at 18:01
Why stringArray[1][j]? Change it to
stringArray[i][j]
– forpas
Nov 28 '18 at 18:01
Why stringArray[1][j]? Change it to
stringArray[i][j]
– forpas
Nov 28 '18 at 18:01
2
2
Integer.parseInt
will not work here as Math.random()
returns a double. What is your intention here?– user7
Nov 28 '18 at 18:01
Integer.parseInt
will not work here as Math.random()
returns a double. What is your intention here?– user7
Nov 28 '18 at 18:01
As per this statement
String stringArray = new String[6][1];
you will have 6 rows with 1 column -- What do you mean by get the maximum value of the second row when there is just 1 element in each row?– Nicholas K
Nov 28 '18 at 18:15
As per this statement
String stringArray = new String[6][1];
you will have 6 rows with 1 column -- What do you mean by get the maximum value of the second row when there is just 1 element in each row?– Nicholas K
Nov 28 '18 at 18:15
add a comment |
1 Answer
1
active
oldest
votes
To find max value in a second dimention you must compare [i][j]
, not [1][j]
for (int i = 0; i < stringArray.length; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < stringArray[i].length; j++) {
int d = Integer.parseInt(stringArray[i][j]);
if (d > max) {
max = d;
}
}
System.out.println("Max row " + i + " value is " + max);
}
Do note that this will print MIN_VALUE
for empty array. For
String stringArray = {{"1", "2", "4"}, {"0", "3", "-1"}};
you will get:
Max row 0 value is 4
Max row 1 value is 3
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525439%2fjava-string-array-get-max%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To find max value in a second dimention you must compare [i][j]
, not [1][j]
for (int i = 0; i < stringArray.length; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < stringArray[i].length; j++) {
int d = Integer.parseInt(stringArray[i][j]);
if (d > max) {
max = d;
}
}
System.out.println("Max row " + i + " value is " + max);
}
Do note that this will print MIN_VALUE
for empty array. For
String stringArray = {{"1", "2", "4"}, {"0", "3", "-1"}};
you will get:
Max row 0 value is 4
Max row 1 value is 3
add a comment |
To find max value in a second dimention you must compare [i][j]
, not [1][j]
for (int i = 0; i < stringArray.length; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < stringArray[i].length; j++) {
int d = Integer.parseInt(stringArray[i][j]);
if (d > max) {
max = d;
}
}
System.out.println("Max row " + i + " value is " + max);
}
Do note that this will print MIN_VALUE
for empty array. For
String stringArray = {{"1", "2", "4"}, {"0", "3", "-1"}};
you will get:
Max row 0 value is 4
Max row 1 value is 3
add a comment |
To find max value in a second dimention you must compare [i][j]
, not [1][j]
for (int i = 0; i < stringArray.length; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < stringArray[i].length; j++) {
int d = Integer.parseInt(stringArray[i][j]);
if (d > max) {
max = d;
}
}
System.out.println("Max row " + i + " value is " + max);
}
Do note that this will print MIN_VALUE
for empty array. For
String stringArray = {{"1", "2", "4"}, {"0", "3", "-1"}};
you will get:
Max row 0 value is 4
Max row 1 value is 3
To find max value in a second dimention you must compare [i][j]
, not [1][j]
for (int i = 0; i < stringArray.length; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < stringArray[i].length; j++) {
int d = Integer.parseInt(stringArray[i][j]);
if (d > max) {
max = d;
}
}
System.out.println("Max row " + i + " value is " + max);
}
Do note that this will print MIN_VALUE
for empty array. For
String stringArray = {{"1", "2", "4"}, {"0", "3", "-1"}};
you will get:
Max row 0 value is 4
Max row 1 value is 3
answered Nov 28 '18 at 18:06
Karol DowbeckiKarol Dowbecki
25.5k93759
25.5k93759
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525439%2fjava-string-array-get-max%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
new String[6][1]
- technically it is a 1D array.– user7
Nov 28 '18 at 18:00
Have you tried using
.length
?– Frontear
Nov 28 '18 at 18:01
Why stringArray[1][j]? Change it to
stringArray[i][j]
– forpas
Nov 28 '18 at 18:01
2
Integer.parseInt
will not work here asMath.random()
returns a double. What is your intention here?– user7
Nov 28 '18 at 18:01
As per this statement
String stringArray = new String[6][1];
you will have 6 rows with 1 column -- What do you mean by get the maximum value of the second row when there is just 1 element in each row?– Nicholas K
Nov 28 '18 at 18:15