I am trying to write a nested for loop that will print out the values of the following code in a specific order:
public static void main(String[] args) {
int[][] array2d = new int[3][5];
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[0].length; j++) {
array2d[i][j] = (i * array2d[0].length) + j + 1;
}
}
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 2; y++) {
System.out.println(array2d[y][x]);
}
}
}
}
The current array prints the way I want it, but each printout on a separate line.
I want the output (on a single line) to be this:
1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
Thanks for the help.
You can use System.out.print instead:
System.out.print(array2d[y][x] + " ");
Replace println with print and it should work
String s = "";
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[i].length; j++) {
s += array2d[i][j] + " ";
}
}
System.out.println(s);
public static void main(String[] args) {
int[][] array2d = new int[3][5];
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[0].length; j++) {
array2d[i][j] = (i * array2d[0].length) + j + 1;
}
}
StringBuilder builder = new StringBuilder();
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 2; y++) {
builder.append(array2d[y][x]);
if(!(x == 4 && y == 2)){
builder.append(" ");
}
}
}
System.out.println(builder.toString());
}
You basically had it right, except for changing the println to be print and formatting the string how you want. I changed it a little to show how the StringBuilder works. Whenever possible I use a StringBuilder because it is more convenient.
Related
I want to basically hide characters following three constant dots (...), the pattern goes like this:
Inputs a phrase from the user and outputs the phrase followed by three dots (...), then the phrase minus one character followed by three dots (...), then the phrase minus two characters followed by the dots, and so on until only one dot is left.
Note: This has to be done using nested for loops only
Sample input
1
disappear
Expected output:
disappear...
disappea...
disappe...
disapp...
disap...
disa...
dis...
di...
d...
...
..
.
This is my attempt:
Problem: I am unable to make it so the phrase decreases each time (minus 1 each time)
I tried using the charAt(); method, but it wouldn't work, I am sure that you would need a for loop separate for each of the dots or a whole set of dots, in this case.
import java.util.Scanner;
public class Dissappear{
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
int option = keyboard.nextInt();
String phrase = keyboard.next();
if (option == 1){
for (int x = 0; x <= phrase.length(); x++){
System.out.print(phrase + "...");
for (int y = 0; y <= phrase.length(); y++){
char n = phrase.charAt(y);
System.out.print(n+"...");
}
}
}
}
}
This is how I got it to work:
public class Disappear {
public static void main(String... args) {
String word = "disappear";
int originalLength = word.length();
for(int i = 0; i < originalLength; i++) {
System.out.println(word.substring(0, originalLength - i) + "...");
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3 - i; j++) {
System.out.print(".");
}
System.out.println();
}
}
}
Without substring:
public class Disappear {
public static void main(String... args) {
String word = "disappear";
int originalLength = word.length();
for(int i = 0; i < originalLength; i++) {
for(int j = 0; j < originalLength - i; j++) {
System.out.print(word.charAt(j));
}
System.out.println("...");
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3 - i; j++) {
System.out.print(".");
}
System.out.println();
}
}
}
You can do it with StringBuilder:
StringBuilder stringBuilder = new StringBuilder(str);
System.out.println(str + "...");
for (int i = 0; i < length; i++) {
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
System.out.println(stringBuilder.toString() + "...");
if (i == length - 1) {
for (int j = 0; j < 2; j++) {
for (int k = j; k < 2; k++) {
System.out.print(".");
}
System.out.println();
}
}
Ok! Nested for loops. But the outer one is only included to meet the requirement. Probably not in the spirit of the assignment though. Just keep decrementing k until it is zero and then latch it there until the StringBuilder length is 0 and the inner loop terminates.
StringBuilder sb = new StringBuilder("disappear...");
for (;;) {
for (int k = sb.length() - 4; sb.length() > 0;) {
System.out.println(sb);
sb.delete(k, k + 1);
k = k > 0 ? --k : 0;
}
break;
}
So I'm currently playing around with multidimensional arrays (2D) and I'm trying to reverse the order of each array in a 2-d array.
So I have a 2D-array set as:
int firstArray[][] = {{5,6,7,8,9,10}, {11,12,13,14,15,16}}
I have manually looked through the issue to see where I may have went wrong, to see which part of my code would end up going out of bounds in regards to my for-loops. The -1 part also caught me off guard.
I have began doing reverses on a regular 1-d array, and tried to apply the same concept to multidimensional arrays.
class Test2 {
public static void main (String[] args) {
int firstArray[][] = {{5,6,7,8,9,10}, {10,11, 12, 13, 14, 15}};
System.out.println("FIRST ARRAY");
display(firstArray);
}
public void display(int [][]num) {
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num[i].length/2; j++) {
int temp = num[i][j];
num[i][j] = num[i][num.length-1-j];
num[i][num.length-1-j] = temp;
}
}
for (int a = 0; a < num.length; a++) {
for (int b = 0; b < num[a].length; b++) {
System.out.print(num[a][b] + "\t");
}
System.out.println();
}
}
}
I want the output using my display method to basically be a reverse of the arrays in my 2-d array:
10 9 8 7 6 5
15 14 13 12 11 10
The issue that I'm getting is an
Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: -1
ArrayIndexOutOfBoundsException: -1
at Test2.display(Test2.java:30)
at Test2.main(Test2.java:20)
You are using the length of the wrong dimension.
With num.length you are using the number of rows and not the number of columns of the current row.
You need to change that to num[i].length.
public static void display(int [][]num) {
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num[i].length/2; j++) {
int temp = num[i][j];
num[i][j] = num[i][num[i].length-1-j];
num[i][num[i].length-1-j] = temp;
}
}
for (int a = 0; a < num.length; a++) {
for (int b = 0; b < num[a].length; b++) {
System.out.print(num[a][b] + "\t");
}
System.out.println();
}
}
Notice you wrote num[i][num.length-1-j];
num.length-1-j is basically 2 - 1 -j.
public static void display(int [][]num) {
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num[i].length/2 ; j++) {
int temp = num[i][j];
num[i][j] = num[i][num[i].length-1-j];
num[i][num[i].length-1-j] = temp;
}
}
for (int a = 0; a < num.length; a++) {
for (int b = 0; b < num[a].length; b++) {
System.out.print(num[a][b] + "\t");
}
System.out.println();
}
}
I want to create a two dimensional array. I am able to compile but not able to run
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; k++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
You have an endless loop: for(j=0;j<5;k++), you have to write for(j=0;j<5;j++)
You increment k instead of j
You have an endless loop. You are incrementing k instead of j:
for(j=0;j<5;k++)
You should change it both times to
for(j=0;j<5;j++)
Here... this should work. Just change your sub-loops making it j++ instead of k++ both top and bottom
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
I think you've mixed up the k and j variables in the second for-loop "block". When I alter it to:
...
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
...
I get the following printed to my console:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
Is it what you wanted?
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
}
you can resolve this problem
I am having some trouble with a Java program. I have a txt data file, which I will display, that I need to add into two separate arrays. The text file is 8 lines long that is supposed to go into two separate 4x4 matrices. A little background info on this program, reads in two arrays, compares them and outputs the largest elements of the same index and outputs them in a separate array. I somehow cannot seem to figure out how to add the data file into two separate arrays. My code is below, thanks in advance.
Data File:
2 7 6 4
6 1 2 4
9 7 2 6
8 3 2 1
4 1 3 7
6 2 3 8
7 2 2 4
4 2 3 1
Code:
public class prog465a
{
public static void main(String[] args) {
Scanner inFile = null;
try
{
inFile = new Scanner(new File("prog465a.dat.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("File not found!");
System.exit(0);
}
int[][] firstData = new int[4][4];
int[][] secondData = new int[4][4];
int[][] finalData = new int[4][4];
for (int i = 0; i< 8; i++)
{
for(int j = 0; j < 8; j++)
{
if (i < 4 && j < 4){ //Trying to add first four lines to one matrix
firstData[i][j] = inFile.nextInt();
} else if (i >= 4 && j >= 4)
{
secondData[i][j] = inFile.nextInt();
}
}
}
for (int i = 0; i< 8; i++)
{
for(int j = 0; j < 8; j++)
{
if (firstData[i][j] >= secondData[i][j])
{
firstData[i][j] = finalData[i][j];
}
else if (secondData[i][j] >= firstData[i][j])
{
secondData[i][j] = finalData[i][j]
}
}
}
for ( int c = 0 ; c < finalData.length ; c++ )
{
for ( int d = 0 ; d < finalData.length ; d++ )
{
System.out.print(finalData[c][d]+" ");
}
System.out.print("\n");
}
}
}
The problems is that you are trying to parse a file with 8 rows and 8 column..
change this:
int[][] finalData = new int[4][4];
for (int i = 0; i< 8; i++)
{
for(int j = 0; j < 8; j++)
to:
int[][] finalData = new int[4][4];
for (int i = 0; i< 8; i++)
{
for(int j = 0; j < 4; j++)
that means 8 rows of file with 4 column each.
Also on the second part of your forloop again you are trying to get 8x8 matrix it should be 4x4..
change:
for (int i = 0; i< 8; i++)
{
for(int j = 0; j < 8; j++)
{
if (firstData[i][j] >= secondData[i][j])
{
firstData[i][j] = finalData[i][j];
}
else if (secondData[i][j] >= firstData[i][j])
{
secondData[i][j] = finalData[i][j]
}
}
}
to:
for (int i = 0; i< 4; i++)
{
for(int j = 0; j < 4; j++)
{
if (firstData[i][j] >= secondData[i][j] )
{
finalData[i][j] = firstData[i][j];
}
else if (secondData[i][j] >= firstData[i][j])
{
finalData[i][j] = secondData[i][j] ;
}
}
}
My professor wants me to print out the matrices side by side with the "+" between the two matrices and then a "=" sign. In the end he wants us to add the matrices together.
This is the work so far.
So the result would come out as:
1 2 3 9 8 7 10 10 10
4 5 6 + 6 5 4 = 10 10 10
7 8 9 3 2 1 10 10 10
enter code here public static void main(String[] args) {
int matrix1[][] = {{1,2,3},{4,5,6},{6,7,8}};
int matrix2[][] = {{9,8,7},{6,5,4},{3,2,1}};
int result1;
int[][] result2 = new int[2][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
System.out.print("");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
}
My problem is, how could I print it side by side with the solutions?
Consider the two printing loops for your matrices:
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
They print matrix 1, then 2 - and so the matrices will be below each other.
If you want the matrices side by side, you need to print line 1 of every matrix, then - after a new line - line 2 of every matrix, etc. By re-arranging how the loops go through the matrices, you could have your new layout.
You unfortunately cannot print them one at a time, you need to take it row by row. This solution requires both matrix1 and matrix2 to be of equal height. But here's a template that should get you started.
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
}
if (i == matrix1/2) {
} else { //One part of if handles when "+" is needed, other one doesn't
}
for (int j = 0; j < matrix2[i].length; j++) {
}
if (i == matrix1/2) {
}
for (int j = 0; j < ???; j++) {
}
}