need help printing out a string in a 2d array [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How to print two dimensional array of strings as String
5 answers
I am trying to print out a 2 dimentional array in a messagedialog using JOptionPane. I am supposed to create a method that converts the array to a string using a for-loop. I have tried a lot, but it doesn't seem to get the logic working as i want it to. This is what i have so far.
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
for (int j = 0; j <array[i].length; j++) {
res += array[i][j];
if(j < array.length-1) {
res += ",";
}
if (i < array.length-1) {
res += "}";
}
}
}res += "}";
return res;
}
Main class:
import javax.swing.JOptionPane;
import arrays.Integer2dArrays;
public class Exercise4b {
public void testArray(int array) {
String message = "";
message += "toString: " + Integer2dArrays.toString( array ) + "n";
message += "elements: " + Integer2dArrays.elements( array ) + "n";
message += "max: " + Integer2dArrays.max( array ) + "n";
message += "min: " + Integer2dArrays.min( array ) + "n";
message += "sum: " + Integer2dArrays.sum( array ) + "n";
message += "average: " + String.format( "%1.2f", Integer2dArrays.average( array ) ) + "n";
JOptionPane.showMessageDialog( null, message );
}
public static void main(String args) {
Exercise4b e4b = new Exercise4b();
int test1 = {{1,2,3,4},{-5,-6,-7,-18},{10,9,8,7}};
int test2 = {{1,2,3,4,5,6},{-7,-8,-9},{2,5,8,11,8},{6,4}};
e4b.testArray(test1);
e4b.testArray(test2);
}
}
The end result should look like this:
java arrays methods tostring
marked as duplicate by luk2302, MC Emperor, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 9:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to print two dimensional array of strings as String
5 answers
I am trying to print out a 2 dimentional array in a messagedialog using JOptionPane. I am supposed to create a method that converts the array to a string using a for-loop. I have tried a lot, but it doesn't seem to get the logic working as i want it to. This is what i have so far.
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
for (int j = 0; j <array[i].length; j++) {
res += array[i][j];
if(j < array.length-1) {
res += ",";
}
if (i < array.length-1) {
res += "}";
}
}
}res += "}";
return res;
}
Main class:
import javax.swing.JOptionPane;
import arrays.Integer2dArrays;
public class Exercise4b {
public void testArray(int array) {
String message = "";
message += "toString: " + Integer2dArrays.toString( array ) + "n";
message += "elements: " + Integer2dArrays.elements( array ) + "n";
message += "max: " + Integer2dArrays.max( array ) + "n";
message += "min: " + Integer2dArrays.min( array ) + "n";
message += "sum: " + Integer2dArrays.sum( array ) + "n";
message += "average: " + String.format( "%1.2f", Integer2dArrays.average( array ) ) + "n";
JOptionPane.showMessageDialog( null, message );
}
public static void main(String args) {
Exercise4b e4b = new Exercise4b();
int test1 = {{1,2,3,4},{-5,-6,-7,-18},{10,9,8,7}};
int test2 = {{1,2,3,4,5,6},{-7,-8,-9},{2,5,8,11,8},{6,4}};
e4b.testArray(test1);
e4b.testArray(test2);
}
}
The end result should look like this:
java arrays methods tostring
marked as duplicate by luk2302, MC Emperor, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 9:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
And what does the end result currently look like?
– achAmháin
Nov 16 '18 at 15:50
Your code prints opening bracket only at the very beginning. start by figuring out when to add the opening bracket at the middle
– Sharon Ben Asher
Nov 16 '18 at 15:56
add a comment |
This question already has an answer here:
How to print two dimensional array of strings as String
5 answers
I am trying to print out a 2 dimentional array in a messagedialog using JOptionPane. I am supposed to create a method that converts the array to a string using a for-loop. I have tried a lot, but it doesn't seem to get the logic working as i want it to. This is what i have so far.
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
for (int j = 0; j <array[i].length; j++) {
res += array[i][j];
if(j < array.length-1) {
res += ",";
}
if (i < array.length-1) {
res += "}";
}
}
}res += "}";
return res;
}
Main class:
import javax.swing.JOptionPane;
import arrays.Integer2dArrays;
public class Exercise4b {
public void testArray(int array) {
String message = "";
message += "toString: " + Integer2dArrays.toString( array ) + "n";
message += "elements: " + Integer2dArrays.elements( array ) + "n";
message += "max: " + Integer2dArrays.max( array ) + "n";
message += "min: " + Integer2dArrays.min( array ) + "n";
message += "sum: " + Integer2dArrays.sum( array ) + "n";
message += "average: " + String.format( "%1.2f", Integer2dArrays.average( array ) ) + "n";
JOptionPane.showMessageDialog( null, message );
}
public static void main(String args) {
Exercise4b e4b = new Exercise4b();
int test1 = {{1,2,3,4},{-5,-6,-7,-18},{10,9,8,7}};
int test2 = {{1,2,3,4,5,6},{-7,-8,-9},{2,5,8,11,8},{6,4}};
e4b.testArray(test1);
e4b.testArray(test2);
}
}
The end result should look like this:
java arrays methods tostring
This question already has an answer here:
How to print two dimensional array of strings as String
5 answers
I am trying to print out a 2 dimentional array in a messagedialog using JOptionPane. I am supposed to create a method that converts the array to a string using a for-loop. I have tried a lot, but it doesn't seem to get the logic working as i want it to. This is what i have so far.
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
for (int j = 0; j <array[i].length; j++) {
res += array[i][j];
if(j < array.length-1) {
res += ",";
}
if (i < array.length-1) {
res += "}";
}
}
}res += "}";
return res;
}
Main class:
import javax.swing.JOptionPane;
import arrays.Integer2dArrays;
public class Exercise4b {
public void testArray(int array) {
String message = "";
message += "toString: " + Integer2dArrays.toString( array ) + "n";
message += "elements: " + Integer2dArrays.elements( array ) + "n";
message += "max: " + Integer2dArrays.max( array ) + "n";
message += "min: " + Integer2dArrays.min( array ) + "n";
message += "sum: " + Integer2dArrays.sum( array ) + "n";
message += "average: " + String.format( "%1.2f", Integer2dArrays.average( array ) ) + "n";
JOptionPane.showMessageDialog( null, message );
}
public static void main(String args) {
Exercise4b e4b = new Exercise4b();
int test1 = {{1,2,3,4},{-5,-6,-7,-18},{10,9,8,7}};
int test2 = {{1,2,3,4,5,6},{-7,-8,-9},{2,5,8,11,8},{6,4}};
e4b.testArray(test1);
e4b.testArray(test2);
}
}
The end result should look like this:
This question already has an answer here:
How to print two dimensional array of strings as String
5 answers
java arrays methods tostring
java arrays methods tostring
edited Nov 16 '18 at 15:49
luk2302
34.6k1773101
34.6k1773101
asked Nov 16 '18 at 15:48
Jacki222Jacki222
32
32
marked as duplicate by luk2302, MC Emperor, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 9:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by luk2302, MC Emperor, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 9:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
And what does the end result currently look like?
– achAmháin
Nov 16 '18 at 15:50
Your code prints opening bracket only at the very beginning. start by figuring out when to add the opening bracket at the middle
– Sharon Ben Asher
Nov 16 '18 at 15:56
add a comment |
And what does the end result currently look like?
– achAmháin
Nov 16 '18 at 15:50
Your code prints opening bracket only at the very beginning. start by figuring out when to add the opening bracket at the middle
– Sharon Ben Asher
Nov 16 '18 at 15:56
And what does the end result currently look like?
– achAmháin
Nov 16 '18 at 15:50
And what does the end result currently look like?
– achAmháin
Nov 16 '18 at 15:50
Your code prints opening bracket only at the very beginning. start by figuring out when to add the opening bracket at the middle
– Sharon Ben Asher
Nov 16 '18 at 15:56
Your code prints opening bracket only at the very beginning. start by figuring out when to add the opening bracket at the middle
– Sharon Ben Asher
Nov 16 '18 at 15:56
add a comment |
2 Answers
2
active
oldest
votes
The common logic you are missing is the
if (i > 0)
res += ",";
So to get it correctly your method toString should be like this:
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
if (i > 0)
res += ",";
res += "{";
for (int j = 0; j <array[i].length; j++) {
if (j> 0)
res += ",";
res += array[i][j];
}
res += "}";
}
res += "}";
return res;
}
Thank you very much maikito! :)
– Jacki222
Nov 16 '18 at 16:08
add a comment |
Maybe you can use deepToString
to achieve your result ?
String result = Arrays.deepToString(test1)
.replace("[", "{")
.replace("]", "}")
.replace(" ", "");
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The common logic you are missing is the
if (i > 0)
res += ",";
So to get it correctly your method toString should be like this:
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
if (i > 0)
res += ",";
res += "{";
for (int j = 0; j <array[i].length; j++) {
if (j> 0)
res += ",";
res += array[i][j];
}
res += "}";
}
res += "}";
return res;
}
Thank you very much maikito! :)
– Jacki222
Nov 16 '18 at 16:08
add a comment |
The common logic you are missing is the
if (i > 0)
res += ",";
So to get it correctly your method toString should be like this:
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
if (i > 0)
res += ",";
res += "{";
for (int j = 0; j <array[i].length; j++) {
if (j> 0)
res += ",";
res += array[i][j];
}
res += "}";
}
res += "}";
return res;
}
Thank you very much maikito! :)
– Jacki222
Nov 16 '18 at 16:08
add a comment |
The common logic you are missing is the
if (i > 0)
res += ",";
So to get it correctly your method toString should be like this:
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
if (i > 0)
res += ",";
res += "{";
for (int j = 0; j <array[i].length; j++) {
if (j> 0)
res += ",";
res += array[i][j];
}
res += "}";
}
res += "}";
return res;
}
The common logic you are missing is the
if (i > 0)
res += ",";
So to get it correctly your method toString should be like this:
public static String toString(int array) {
String res = "{";
for (int i = 0; i < array.length; i++) {
if (i > 0)
res += ",";
res += "{";
for (int j = 0; j <array[i].length; j++) {
if (j> 0)
res += ",";
res += array[i][j];
}
res += "}";
}
res += "}";
return res;
}
edited Nov 16 '18 at 16:10
answered Nov 16 '18 at 16:04
maikitomaikito
162
162
Thank you very much maikito! :)
– Jacki222
Nov 16 '18 at 16:08
add a comment |
Thank you very much maikito! :)
– Jacki222
Nov 16 '18 at 16:08
Thank you very much maikito! :)
– Jacki222
Nov 16 '18 at 16:08
Thank you very much maikito! :)
– Jacki222
Nov 16 '18 at 16:08
add a comment |
Maybe you can use deepToString
to achieve your result ?
String result = Arrays.deepToString(test1)
.replace("[", "{")
.replace("]", "}")
.replace(" ", "");
add a comment |
Maybe you can use deepToString
to achieve your result ?
String result = Arrays.deepToString(test1)
.replace("[", "{")
.replace("]", "}")
.replace(" ", "");
add a comment |
Maybe you can use deepToString
to achieve your result ?
String result = Arrays.deepToString(test1)
.replace("[", "{")
.replace("]", "}")
.replace(" ", "");
Maybe you can use deepToString
to achieve your result ?
String result = Arrays.deepToString(test1)
.replace("[", "{")
.replace("]", "}")
.replace(" ", "");
answered Nov 16 '18 at 15:53
Schidu LucaSchidu Luca
3,040522
3,040522
add a comment |
add a comment |
And what does the end result currently look like?
– achAmháin
Nov 16 '18 at 15:50
Your code prints opening bracket only at the very beginning. start by figuring out when to add the opening bracket at the middle
– Sharon Ben Asher
Nov 16 '18 at 15:56