How to stop printing null in an array - java
How do I stop my program from printing out the positions of the array that are null, when I'm going through an array and printing the positions out?
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]);
}
It's simple like this, but how do I stop it from printing out the positions that are null?
Thanks in advance for an answer to this simple question...
for(int i=0;i<array.length;i++)
{
if (array[i]!=null) {
System.out.println(array[i]);
}
}
You just use the i of the for loop to check inside the array what the value is.
I hope this is what you needed.
for (int i = 0; i < array.length; i++)
{
if(array[i] != null)
{
System.out.print(array[i]);
}
}
Related
Different ways to iterate 2 dimensional array
I'm preparing for the Java OCA exam but there are several tricky questions about how iterates a multidimensional array. So, if I had this array and I wanted to iterate using the for and for-each loops what would all be ways to do it? I usually used only these three: int [][]matrix = {{3,4,5},{6,7,8},{9},{10,11,12}}; //First way for (int [] a : matrix){ for (int i =0; i<a.length;i++){ //code } } //Second way for (int []a: matrix){ for (int i: a){ //code } } //Third way for (int i = 0; i<matrix.length; i++) { for (int j=0; j<matrix[a].length; j++) { //code } } //Fourth way??? Thanks a lot!
You missed the for, foreach combination: for (int i = 0; i<matrix.length; i++) { for (int j: matrix[i]){ // code } } You can also use while loops instead of for. Can you be more specific about the context, so we can help you properly with what you need?
Print ArrayList of Arrays in Java
I created an ArrayList of arrays, but I don't know how to print them. Here's what I am trying to do: int j=0; for (int i=0; i<aArray.length-1; i++){ exp=new int[100]; if (Character.isDigit(aArray[i])){ if(Character.isDigit(aArray[i])==true && aArray[i-1]=='-'){ exp[j]=Character.getNumericValue(-(aArray[i])); } else{ exp[j]=Character.getNumericValue(aArray[i]); } System.out.print(exp[j]); j++; } } System.out.println(" "); list.add(exp); } for (int i=0; i<5; i++){ System.out.println(list.get(i)[i]); } All I get is 5 zeros. What am I missing? Practically, I read a .txt file in my program, and all I want to do, is seperate and write the numbers of each in the exp[] arrays and then save them in list (ArrayList of Arrays).
Add a for loop to go through each array in the list too: for (int i=0; i<5; i++){ int[] tmp = list.get(i); for (int j=0; j<tmp.length; j++) { System.out.println(tmp[j]); } } edit: You also need to be adding the array to the arraylist inside the for loop, otherwise you're adding a null array. for (int i=0; i<aArray.length-1; i++){ exp=new int[100]; if (Character.isDigit(aArray[i])){ if(Character.isDigit(aArray[i])==true && aArray[i-1]=='-'){ exp[j]=Character.getNumericValue(-(aArray[i])); } else{ exp[j]=Character.getNumericValue(aArray[i]); } System.out.print(exp[j]); j++; } System.out.println(" "); list.add(exp); }
Problem solved guys, I just needed to initialize exp array outside "for" loop, place a "while" loop and that's all! Thank you for your answers!
Array Index out of range exception
I want to print out the elements of this multidimensional array but I get the index out of range error. public class test { public static void main(String[] args) { int array1[][]={{1,2,3,4},{5},{6,7}}; for (int i=0; i<3;i++){ for (int j=0; j<4;j++){ System.out.println(array1[i][j]); } } } }
The problem is that with the loop, written like this, you assume that the nested arrays are all of length of 4 (and they are not). You'd better do: for (int i=0; i < array1.length;i++) { for (int j=0; j < array1[i].length;j++) { ... } }
Yes, because the second "subarray" doesn't have 4 elements. It would be better to do this dynamically: // Note preferred syntax for array types - keep all the type info in one place. int[][] array1 = {{1,2,3,4},{5},{6,7}}; for (int i = 0; i < array1.length; i++) { for (int j = 0; array1[i].length; j++) { System.out.println(array1[i][j]); } } This way the iteration count of the inner loop depends on the array being iterated over. An alternative is to use the enhanced for loop: for (int[] subarray : array1) { for (int value : subarray) { System.out.println(value); } }
2 points regarding the following solution (and the other similar answers): for (int i=0; i < array1.length;i++) { for (int j=0; j < array1[i].length;j++) { ... } } Here it wont make much difference, as the code is very short, but for better performance you should always avoid repeated calculations if the result never changes. array1's length wont change so calculating its length every iteration is not efficient. This would be improved solution. int array1Length = array1.length; for (int i=0; i < array1Length;i++){ int array1InnerLength = array1[i].length; for (int j=0; j < array1InnerLength;j++){ ... } } Some people may suggest using ++j / ++i instead of i++ and j++ for better performance, you can read more about it here: What is the difference between ++i and i++?. Its not that critical imo, but it improves understanding of the code you're writing.
How would I display the position of an element in an array?
My dilemma is after the user inputs a number, that number is then checked to see if it's in the array, if it is i'll let them know that is in the array along with the position of that said number. I have it to where it prompts user for the number, but after that i get the ArrayIndexOutOfBoundsException error. Here's what I have so far: int [] iGrades = new int [30]; System.out.print ("Enter the grades, when you're done input (-1) "); for (int i=0; i<iGrades.length;i++) { iGrades [i]= kb.nextInt(); if (iGrades [i]< 0) { break; } } System.out.print ("\nEnter a grade to check if it's in the array"); iVal = kb.nextInt(); for(int i=0; i<=iGrades.length; ++i) { if(iVal == (iGrades[i])) { found = true; for(int j=0; j<=iGrades.length; ++j) { iGrades[j]=j+1; } break; } } if (found == true) { System.out.println(iVal + " is in the array at position "); } else { System.out.println(iVal + " is NOT in the array."); } } Any assistance would be great.
The problem is in your second for loop. This for(int i=0; i<=iGrades.length; ++i) Change the <= to <.
The exception says it already. You are checking an index that is not in the bounds of the array any more. Look at the second for loop: for(int i=0; i<=iGrades.length; ++i) It runs from 0 to 30. The array only goes from 0 to 29 though. You have to use < instead here: for(int i=0; i < iGrades.length; i++)
Since arrays are zero-based, this i<=iGrades.length will allow i to equal Grades.length, which is one past the last index of the array. Use <.
Remember Array Index start from 0 to length of array -1 Change the loop accordingly Hint change <= to <
For loop with substrings
Beginning CS student here, trying to get a handle on loops. My task is to take a String s, such that if s = 'abcd', the program will print: 'a,b,c,d,ab,bc,cd,abc,bcd,abcd'. Clearly the begin index and end index change as the loop iterates—(0,1),(1,2),(2,3),(3,4). That's fine and I can print: 'a,b,c,d'. But how do I control it such that it will go from that to (0,2),(1,3),(2,4) and then (0,3),(1,4), and finally (0,4)? This is where I am stumped. Thanks for the assistance and here's my code: void printSubstring() { int len = s.length(); for (int i=0;i<len;i++) { for (int k=0;k<len;k++) System.out.print(s.substring(k,k+1)+", "); } System.out.println(); }
for (int i=0;i<len;i++) { for (int k=0;k<len-i;k++) System.out.print(s.substring(k,k+i+1)+","); } This as your for structure should work. At each step inner loop, the number of strings to print decreases (using -i). And the length of the strings increase (using +i in the substring) Tested with: abcdefghijklmnopqrstuvwxyz Output: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,ab,bc,cd,de,ef,fg,gh,hi,ij,jk,kl,lm,mn,no,op,pq,qr,rs,st,tu,uv,vw,wx,xy,yz,abc,bcd,cde,def,efg,fgh,ghi,hij,ijk,jkl,klm,lmn,mno,nop,opq,pqr,qrs,rst,stu,tuv,uvw,vwx,wxy,xyz,abcd,bcde,cdef,defg,efgh,fghi,ghij,hijk,ijkl,jklm,klmn,lmno,mnop,nopq,opqr,pqrs,qrst,rstu,stuv,tuvw,uvwx,vwxy,wxyz,abcde,bcdef,cdefg,defgh,efghi,fghij,ghijk,hijkl,ijklm,jklmn,klmno,lmnop,mnopq,nopqr,opqrs,pqrst,qrstu,rstuv,stuvw,tuvwx,uvwxy,vwxyz,abcdef,bcdefg,cdefgh,defghi,efghij,fghijk,ghijkl,hijklm,ijklmn,jklmno,klmnop,lmnopq,mnopqr,nopqrs,opqrst,pqrstu,qrstuv,rstuvw,stuvwx,tuvwxy,uvwxyz,abcdefg,bcdefgh,cdefghi,defghij,efghijk,fghijkl,ghijklm,hijklmn,ijklmno,jklmnop,klmnopq,lmnopqr,mnopqrs,nopqrst,opqrstu,pqrstuv,qrstuvw,rstuvwx,stuvwxy,tuvwxyz,abcdefgh,bcdefghi,cdefghij,defghijk,efghijkl,fghijklm,ghijklmn,hijklmno,ijklmnop,jklmnopq,klmnopqr,lmnopqrs,mnopqrst,nopqrstu,opqrstuv,pqrstuvw,qrstuvwx,rstuvwxy,stuvwxyz,abcdefghi,bcdefghij,cdefghijk,defghijkl,efghijklm,fghijklmn,ghijklmno,hijklmnop,ijklmnopq,jklmnopqr,klmnopqrs,lmnopqrst,mnopqrstu,nopqrstuv,opqrstuvw,pqrstuvwx,qrstuvwxy,rstuvwxyz,abcdefghij,bcdefghijk,cdefghijkl,defghijklm,efghijklmn,fghijklmno,ghijklmnop,hijklmnopq,ijklmnopqr,jklmnopqrs,klmnopqrst,lmnopqrstu,mnopqrstuv,nopqrstuvw,opqrstuvwx,pqrstuvwxy,qrstuvwxyz,abcdefghijk,bcdefghijkl,cdefghijklm,defghijklmn,efghijklmno,fghijklmnop,ghijklmnopq,hijklmnopqr,ijklmnopqrs,jklmnopqrst,klmnopqrstu,lmnopqrstuv,mnopqrstuvw,nopqrstuvwx,opqrstuvwxy,pqrstuvwxyz,abcdefghijkl,bcdefghijklm,cdefghijklmn,defghijklmno,efghijklmnop,fghijklmnopq,ghijklmnopqr,hijklmnopqrs,ijklmnopqrst,jklmnopqrstu,klmnopqrstuv,lmnopqrstuvw,mnopqrstuvwx,nopqrstuvwxy,opqrstuvwxyz,abcdefghijklm,bcdefghijklmn,cdefghijklmno,defghijklmnop,efghijklmnopq,fghijklmnopqr,ghijklmnopqrs,hijklmnopqrst,ijklmnopqrstu,jklmnopqrstuv,klmnopqrstuvw,lmnopqrstuvwx,mnopqrstuvwxy,nopqrstuvwxyz,abcdefghijklmn,bcdefghijklmno,cdefghijklmnop,defghijklmnopq,efghijklmnopqr,fghijklmnopqrs,ghijklmnopqrst,hijklmnopqrstu,ijklmnopqrstuv,jklmnopqrstuvw,klmnopqrstuvwx,lmnopqrstuvwxy,mnopqrstuvwxyz,abcdefghijklmno,bcdefghijklmnop,cdefghijklmnopq,defghijklmnopqr,efghijklmnopqrs,fghijklmnopqrst,ghijklmnopqrstu,hijklmnopqrstuv,ijklmnopqrstuvw,jklmnopqrstuvwx,klmnopqrstuvwxy,lmnopqrstuvwxyz,abcdefghijklmnop,bcdefghijklmnopq,cdefghijklmnopqr,defghijklmnopqrs,efghijklmnopqrst,fghijklmnopqrstu,ghijklmnopqrstuv,hijklmnopqrstuvw,ijklmnopqrstuvwx,jklmnopqrstuvwxy,klmnopqrstuvwxyz,abcdefghijklmnopq,bcdefghijklmnopqr,cdefghijklmnopqrs,defghijklmnopqrst,efghijklmnopqrstu,fghijklmnopqrstuv,ghijklmnopqrstuvw,hijklmnopqrstuvwx,ijklmnopqrstuvwxy,jklmnopqrstuvwxyz,abcdefghijklmnopqr,bcdefghijklmnopqrs,cdefghijklmnopqrst,defghijklmnopqrstu,efghijklmnopqrstuv,fghijklmnopqrstuvw,ghijklmnopqrstuvwx,hijklmnopqrstuvwxy,ijklmnopqrstuvwxyz,abcdefghijklmnopqrs,bcdefghijklmnopqrst,cdefghijklmnopqrstu,defghijklmnopqrstuv,efghijklmnopqrstuvw,fghijklmnopqrstuvwx,ghijklmnopqrstuvwxy,hijklmnopqrstuvwxyz,abcdefghijklmnopqrst,bcdefghijklmnopqrstu,cdefghijklmnopqrstuv,defghijklmnopqrstuvw,efghijklmnopqrstuvwx,fghijklmnopqrstuvwxy,ghijklmnopqrstuvwxyz,abcdefghijklmnopqrstu,bcdefghijklmnopqrstuv,cdefghijklmnopqrstuvw,defghijklmnopqrstuvwx,efghijklmnopqrstuvwxy,fghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuv,bcdefghijklmnopqrstuvw,cdefghijklmnopqrstuvwx,defghijklmnopqrstuvwxy,efghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvw,bcdefghijklmnopqrstuvwx,cdefghijklmnopqrstuvwxy,defghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvwx,bcdefghijklmnopqrstuvwxy,cdefghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvwxy,bcdefghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvwxyz,
Inner loop should be - for (int k=0;k<len-i;k++) System.out.print(s.substring(k,k+i+1)+", "); Hope that helps
You could try something like this, void printSubstring(String s){ if(s == null){ return; } for(int lenghtSubstring = 1; lenghtSubstring <= s.length(); lenghtSubstring++){ for(int index = 0; index <= s.length() - lenghtSubstring; index++){ System.out.print(s.substring(index, index + lenghtSubstring) + ","); } } }
void printSubstring() { int len = s.length(); for (int i=1;i<len+1;i++) { for (int k=0;k<len-i;k++) System.out.print(s.substring(k,k+i)+", "); } System.out.println(); }