Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a JAVA code which prints a pattern. But I am not getting the output which I am looking for.
Filename -> test.java
public class test
{
public static void main(String args[])
{
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++);
{
System.out.print("1 ");
}
System.out.println();
}
}
}
It should print this
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
But it is printing this as output
1
1
1
1
1
Please rectify if there are any errors
You added ; in the second loop. That is why printing like this. Your eclipse code must be no ; there. :)
public class test
{
public static void main(String args[])
{
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++);
{
System.out.print("1 ");
}
System.out.println();
}
}
}
It's because you have a ; after for(j=0; j<=i; j++). Eclipse must be assuming you don't intend it to be there while other IDEs are not. Delete the ; and it runs fine.
Removed ; after the second for loop.
public class test
{
public static void main(String args[])
{
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)
{
System.out.printf("i ");
}
System.out.println();
}
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
The below pattern code is not working as expected.
public class pattern_print {
public static void main (String args[]){
int i = 1, j = 5, n = 5;
while (i <= n) {
while (j >= i) {
System.out.print("*");
j--;
}
System.out.print("\n");
i++;
}
}
}
Who can help me?
What are you expecting?
The code that you wrote is displaying the following chars:
*****
If you want to display something like:
*****
****
***
**
*
Then the correct code is:
public class pattern_print {
public static void main (String args[]) {
int i = 1, j = 5, n = 5;
while (i <= n) {
while (j >= i) {
System.out.print("*");
j--;
}
System.out.print("\n");
j=5;
i++;
}
}
}
Now depends what you expect to be displayed.
If the following triangle pattern is expected:
*****
****
***
**
*
the value of j needs to be reset to n as Andreea Frincu suggested.
However, for loops may be more preferable when printing patterns.
Also, since Java 11 released back in Sep 2018 there is method String::repeat which allows to avoid redundant nested loops and the same triangle pattern may be printed as simply as:
for (int i = 5; i >= 0; i--) {
System.out.println("*".repeat(i));
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
/* the followng main method calls a sortBySelection() method wich shall return an array, but when printing the array directly from the calling statement i am getting wrong stuff!
the array is being returned and printed correctly using a for loop
if i am to use the Arrays class to print the returned array, java is not accepting my code... what is wrong with the code?
*/
import java.util.Arrays;
public class xyz {
public static void main(String [] args){
int [] list = {1,7,4,5,12,205,11,0,1,52,32,3, 27, 72,10, 19, 16};
//The following line is not printing the returned array!!
System.out.print("\nthe sorted array is: " +
(sortBySelection(list)+"\n"));
//The following statement is not even accepted by java!!!
System.out.print("\nthe sorted array is: " +
Arrays.toString((sortBySelection(list))+"\n"));
//The following for loop prints the returned array correctly!!!
for (int i=0; i<list.length; i++)
System.out.print(list[i]+" ");
for (int i=0; i<list.length; i++)
System.out.print(list[i]+" ");
}
public static int[] sortBySelection(int[] array){
int temp = 0;
for (int j=array.length-1; j>=0; j--){
for (int i=0 ; i<j; i++){
if (array[i] > array[j]){
//swap
temp = array[j];
array[j] =array[i];
array[i] = temp;
}
}
}
return array;
}
}
System.out.print("\nthe sorted array is: " +
Arrays.toString((sortBySelection(list))+"\n"));
The problem with this is you are calling Arrays.toString on a String. You can fix by fixing the parentheses
System.out.print("\nthe sorted array is: " +
Arrays.toString(sortBySelection(list))+"\n");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
public class Main {
public static void main(String[] args) {
int z;
int [] a = new int[5];
a[0]=4;
a[1]=8;
a[2]=5;
a[3]=1;
a[4]=3;
for(;;){
z=0;
for(int i=1;i<a.length;i++){
if(a[i-1]>a[i]){
int tmp = a[i];
a[i-1]=a[i];
a[i]=tmp;
z++;
}
}
if(z==0){
break;
}
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}}
Hi. I have this problem. I want to sort array items, but result of this code is 1 1 1 1 3. I can't understand where is problem.
Thanks you very much!
You overwrite the value of a[i-1] thus you lose it
So you have just to change this int tmp = a[i]; with this int tmp = a[i-1];
The whole code is :
public class MailClient {
public static void main(String[] args) {
int z;
int [] a = new int[5];
a[0]=4;
a[1]=8;
a[2]=5;
a[3]=1;
a[4]=3;
for(;;){
z=0;
for(int i=1;i<a.length;i++){
if(a[i-1]>a[i]){
int tmp = a[i-1];
a[i-1]=a[i];
a[i]=tmp;
z++;
}
}
if(z==0){
break;
}
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I try to print the odd numbers in Java that are inside the array but this algorithm doesn't work ... May someone help me ?
The printing result is that :
"Exception in thread "main" .java.lang.ArrayIndexOutOfBoundsException: 7
at JavaArray.main(JavaArray.java:12)"
Code :
public class JavaArray {
public static void main(String[] args) {
int[] myArray = {1,3,4,5,8,9,10};
int i = 0;
for(i = 0; i < myArray.length; i++); {
if(myArray[i] % 2 == 1) {
System.out.println(myArray[i]);
}
}
}
}
Remove the semi-colon that is terminating your for loop
for (i = 0; i < myArray.length; i++);
^
Because you have placed semicolon after for loop, variable i increments till length of array(here 7). After that loop ends and you are trying to access myarray element through i which is 7 so it is giving out of bound exception.
Besides the extra ; you need to remove, you can consolidate by declaring the int in the loop declaration:
for (int i = 0; i < myArray.length; i++) {
.
.
.
}
Beside #Reimus point , you can also do it like below , sort the array if it's not sorted yet, in your case it is sorted . FYI, Instead of Collections.sort which is above O(N) complexity use a Hash Set.
public static void main(String[] args) {
int[] myArray={1,3,4,5,8,9,10};
Arrays.sort(str);
for (int i = 1; i < myArray.length; i++) {
if (str[i] == str[i - 1]) {
System.out.println("Dupe-num: " + str[i];
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This program is giving me array out of bounds exception :100
How to resolve??? also tell me whether my printing method of array is correct or not?
import java.util.Random;
import java.lang.Math;
class MersennePrime {
public int[] MersennefindPrime() {
int i=0;
int k=0;
int array[] = new int[100];
for(i=2;i<100;i++)
{
int count=0;
for(int j=2;j<=Math.sqrt(i);j++ )
{
if(i%j==0)
{
}
else
{
array[k]=i;
k++ ;
}
}
}
return array;
}
}
public class MersenneRandomNumbers
{
public static void main(String[] args)
{
MersennePrime mrn = new MersennePrime();
int array[] =mrn.MersennefindPrime();
for(int s=0;s<=array.length;s )
System.out.println("array is " array[s]);
}
}
}
This
for(int s=0;s<=array.length;s )
System.out.println("array is " array[s]);
}
should be like this
for(int s=0; s<array.length; s++) {
System.out.println( "array is " + array[s] );
}
Note the change from <= to <!
In your code, within the last loop s equals array.length, which is just the first index outside the boundaries of the array.
EDIT
Beside the upper syntactic error, there is a logical one here as well. In MersennefindPrime() the outer loop runs from 0 to 100, while the inner loop runs ("worst case") from 2 to 10. So there might be about 10 * 100 times, where you increase k and try to set the respective index in the array. This is far more, than the 100 items you allocated the array for!
If you can't be sure about the extent of your array at initialization, use some class, that implements the interface List. This could then look like this:
public List<Integer> MersennefindPrime() {
ArrayList<Integer> array = new ArrayList<Integer>();
for(int i=2;i<100;i++)
{
for(int j=2;j<=Math.sqrt(i);j++ )
{
if(i%j==0)
{
}
else
{
array.add( i );
}
}
}
return array;
}
You would have to adjust the code in main() accordingly!