Below pattern code is not working as expected [closed] - java

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));
}

Related

Java Programming - Coding based on flowchart [closed]

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 2 years ago.
Improve this question
I am new to programming and would appreciate any experts to provide suggestions.
Based on the given flowchart, am I on the right track?
How do I make improvement to my code to ensure robustness?
Flowchart
Code
// r is row, c is column
int r = 1, c = 1;
do {
if (r <= 4)
{
if (c <= 10)
{
System.out.print("*");
c += 1;
}
else {
r += 1;
c = 1;
System.out.println();
}
}
else {
System.exit(1);
}
}while (c != 12);
Output
**********
**********
**********
**********
while (c != 12);
The flow chart doesn't have this condition.
Notice how your if (r <= 4) statement is directly nested in the do...while. This means you can reduce to a single while:
while (r < = 4) {
...
}
Similarly c <= 10 should be implemented as a loop instead of just an if.
The flow chart doesn't say to do start a new line like you do with System.out.println();. So taken literally, this line is incorrect. However, I suspect the flow chart omitted this detail and is in error.
According to the algorithm proposed, its code is seen to follow or comply with the guideline.
Your code looks great, it just tries to name the variables in a more similar way to what you are trying to replicate.
I have tried to shorten the number line of coding.
int row = 1, column = 1;
while (row <= 4)
{
if (column <= 10) {
System.out.print("*");
column += 1;
}
else {
row += 1;
column = 1;
//System.out.println();
}
}

Not getting a desired output in JAVA as expected [closed]

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();
}
}
}

To use method from the main method java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
This question is pretty specific for my problem, that is why I am creating a new question. The second method in this program is supposed to make a row of the number 1 2 3 4 5 6 7 8 9 10. The only problem I am having is that I don't know how to print this out in the main method.
public class Uppgift1_6a
{
public static void main(String[] args)
{
for(int k = 0; k < 10; k++)
{
int tal = Numberline(k);
System.out.print(tal);
}
}
public static int Numberline(int tal1)
{
int tal = 1;
for(int i = 1; i < 11; i++)
{
tal = tal1 + i;
}
return tal;
}
}
Right now it prints out all the number from 11 to 19. And if I change it, it only prints out either 10 or 11.
Look closely at the code:
public static int Numberline(int tal1)
{
int tal = 1;
for (int i = 1; i < 11; i++)
{
tal = tal1 + i;
}
return tal;
}
The for loop literally does absolutely nothing - you're only returning the final result. The final result is always exactly equal to tal1 + 10; again, what the for loop did up this point makes no difference. (I'd encourage you to step through the code with a debugger to convince yourself of that fact).
If you want it to print out the values as you're going through the for loop, you need to do something like:
for (int i = 1; i < 11; i++)
{
// You may need to modify this line too, depending on what values you want printed
tal = tal1 + i;
// Print the value here
System.out.print(tal);
}
because the way you've written it it'll only print out the final value of tal (the one you returned).

Java printing patterns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java.
I have next code:
public static void main(String[] args) {
for(int k = 10; k > 0; k--)
{
for(int l=0; l < k-1; l++)
{
System.out.print(' ');
}
for(int n=10; n > k-1; n--)
{
System.out.print('*');
}
System.out.println();
}
}
It prints this:
*
**
***
****
*****
******
*******
********
*********
**********
But I want to print it with empty inside like this:
*
**
* *
* *
* *
* *
* *
* *
* *
**********
Can anyone to explane me how to do it.
I understand that this is not a place where solve homework tasks. But can somebody tell me the algorithm for solving the problem in words.
I do not need a ready solution because I want to understand and solve it by myself. So how I can put spaces inside?
Hint: In the second loop you should print a '*' for the first and last values of n, not all of them. For the other positions, print a ' '.
Hint: in the second inner for loop, you could have an if statement where you check for n's greatest and lowest values, and k's lowest value, then print a '*', otherwise print a ' '. Like so:
for(int n = 10; n > k - 1; n--){
if(n == 10 || n == k || k == 1){
System.out.print('*');
} else {
System.out.print(' ');
}
}

finding odd numbers in java [closed]

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];
}
}
}

Categories

Resources