How can I make a specific Java output with *? [closed] - java

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 7 years ago.
Improve this question
How can I make this output in Java in a for-loop? How can I make every line less "*" ?
Input:
enter number: 6
Java output:
******
*****
****
***
**
*

This seems a homework assignment, and it's nice to you to achieve this little goals to correct programming learning. As we're not here to make your homework, here you have few steps to guide you through:
Define a Scanner to ask user's input
Use Scanner to put user's input into a variable
Use this variable in a inverted for-loop (with loopCounter--)
for (int loopCounter = userInputVariable; loopCounter > 0; loopCounter --)
To repeat the * use:
StringUtils String repeated = StringUtils.repeat("*", loopCounter); or
for loop to repeat the char
for (int innerLoopCounter = 0; innerLoopCounter < loopCounter; innerLoopCounter ++)

Something like this. Please bear in mind that I don't know java.
for (int i = input; i > 0; i--) {
System.out.println(StringUtils.repeat("*", i));
}

Related

Can anyone tell me what is the problem with this piece of code in Android Studio? [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 2 years ago.
Improve this question
I want to check if the length of the string is >3 and if there are more than two spaces.
I implemented a for loop to count how many spaces are there(code below).
Then used the 'if'.
When I add &&, the app crashes when there are less than 3 characters.
It works when I remove the && part but, I need it! :/
Is it a problem with the logic I have used? :/
for loop:
for(int i=0; i<4; i++){
if(message.charAt(i) == ' '){
count++;
}
}
then the if part:
if(message.length()<3 && count>2){
outputText.setText("Duh! DO NOT TRY TO FIGURE THIS OUT!");
Toast.makeText(getApplicationContext(),"WARNING!!",Toast.LENGTH_SHORT).show();
}
else
methodEncrypt(message);
NomadMaker is correct. One reason the loop may be failing is the assumption that the length of 'message' is greater than 4. If message = '123', then attempting to get the 'charAt' index 4 will fail due to the requested index being greater than the size of the String.
Updating to:
for(int i=0; i < message.length(); i++)
{
if(message.charAt(i) == ' ')
{
count++;
}
}
Should resolve this issue. Some minor performance improvements may also be considered. Ex:
If the length of the String is less than 4, don't bother iterating through the String.
Once the second space is found, don't bother iterating through the rest of the String.

Simple java: How can I replace all number 5's in this array with 0's? [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 2 years ago.
Improve this question
https://gyazo.com/fd643045f8f8eb285b25142013095947
What can I add to this code, to replace all of the number 5 with 0?
Thanks!
Loop over the array, check if the number is 5, and if so set them to 0
for(int i = 0; i < arr.length; i++) {
if(arr[i] == 5) arr[i] = 0;
}

Java calculate 1*3*5*7*...* to user given double [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 3 years ago.
Improve this question
i am trying to make java program that ask number from user and runs following calculation: 1*3*5*7*....*usergivendouble. I think for loop is best for this but not sure how to make such loop. i tried
for(double i=1;i<=n;i+=2)
{
n*= 2;
}
but it just never stops asking new number.
im new to java and all help is appreciated!
Assuming n is the user given number, increasing n inside the loop is the problem. You increase n inside the loop and also use it as the loop's end condition. This causes an infinite loop, as the loop's condition is never met.
You need to change the code to be:
double multiplyRestult=1;
for(double i=1;i<=n;i+=2)
{
multiplyRestult*= i;
}

(Java) Go thru array beginning at the last index [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 currently trying to make a method in Java that takes an array of size digits, and is supposed to go thru the array starting at the end, adding 1 to digits <= 8, and turning 9's into 0's.
My problem is that I'm not quite sure how to start going through the array beginning at the end. I'm coming off of Python, so I'm familiar with the syntax list[:-1], but I'm not sure how to apply that, or if it can be applied, to Java.
Thank you very much in advance.
Use this:
public void someMethod(int[] arr){
for(int i=arr.length-1; i >= 0; i--){
if(arr[i] <= 8){
arr[i]+=1;
}else if(arr[i] ==9){
arr[i] = 0;
}
}
}
Refer https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for better undersatnding of for loop construct.
Java has unfortunately no operation like list[:-1] so you will need to do a reversed for loop or a decremental lopp to achieve that:
for (int i = value; i > 0; i--) {
System.out.println("Am decreasing over the element: " + i);
}
Also, please take a look at the Oracle tutorial about for loops

Star pyramid with while loop [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 8 years ago.
Improve this question
I am currently puzzled on making a star pyramid.
My product needs to be able to create a pyramid with a user-inputted # of rows. So if I say 3, the pyramid would look like:
*
***
*****
When I enter 3, my pyramid (based on my code below) looks like:
*
***
*****
I'm having trouble making the spaces get deducted when going down a row. I seem to have other parts down accurately, so my question is, how do I deduct spaces for each following row by 1 after the initial amount of spaces? Could I get help on how to fix my code? (while using a while loop). Feel free to comment on any other parts of my code if it seems inaccurate though.
At the moment this is what my code looks like:
import java.util.Scanner;
public class AstPyramid{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("How tall do you want your pyramid to be? Rows: ");
int rowCounter = keyboard.nextInt();
int baseLength = 1;
int starsInRow = 1;
while (baseLength <= rowCounter){
int starCounter = 1;
int whiteSpace = rowCounter/2;
while (whiteSpace >= 0){
System.out.print(" ");
whiteSpace--;
}
while (starCounter <= starsInRow){
System.out.print("*");
starCounter++;
}
System.out.println();
baseLength++;
starsInRow=starsInRow+2;
}
}
}
Update: I updated this question many times and already have received an answer pertaining to it. Could this question be un-held now? I'm not sure if editing this post notifies anybody, but it is worth another try.
In your code, the first inner-loop that you are using to print the white space, always runs for rowCounter / 2 times. That is the problem. It should ideally start at that value, and keep decreasing for each iteration of the outer loop.
You could have a spacesPerRow variable and assign it an initial value of rowCounter / 2, outside the outer loop. Then on each outer-loop iteration, you can decrease it. And use this spacesPerRow in your first inner loop to print the spaces.
Your spacesPerRow should be decremented once for each iteration of the outer loop. An ideal place would be right after your first inner loop finishes.
To moderators & question down-voters:
I can't add comments, so I'll add my thoughts here. The first few lines of the question precisely mention the problem: "I want to do X, but my code is doing Y". And the OP is not able to figure out why.
I also believe the code doesn't include a lot of unnecessary parts either.
I am wondering why this is considered off-topic, put on hold, and has two down-votes.

Categories