Need help making code for equilateral triangle in java work - java

For some reason only the odd numbered lines are spacing correctly. Can someone please explain this? Here is my code.
import java.util.Scanner;
public class Triangle {
public static void main(String args[]) {
int i, j, k1;
Scanner in = new Scanner(System.in);
System.out.println("Enter the length of the side of your equilateral triangle: ");
int side = in.nextInt();
System.out.println("Enter the character you want your triangle filled with: ");
char fill = in.next().charAt(0);
while (side<=0 || side>50){
System.out.print("Please enter a length between 1 and 50: ");
side = in.nextInt();
break;
}
for(i=1; i<=side;i++){
for (k1=0; k1 < (side-i/2);k1++){
System.out.print("\t");
}
for(j=1;j<=i;j++){
System.out.print(fill + "\t");
}
System.out.println('\n');
}
in.close();
}
}
This is my outcome:
Enter the length of the side of your equilateral triangle:
5
Enter the character you want your triangle filled with:
*
*
* *
* * *
* * * *
* * * * *
I want this:
#
# #
# # #
# # # #
# # # # #

Just add this and change /t to 4 spaces:
if(i%2 ==0 && j ==1){
System.out.print(" "); // two spaces
}
Full code
for(i=1; i<=side;i++){
for (k1=0; k1 < (side-i/2);k1++){
System.out.print(" ");
}
for(j=1;j<=i;j++){
if(i%2 ==0 && j ==1){
System.out.print(" ");
}
System.out.print(fill + " ");
}
System.out.println('\n');
}
The reason is because every even line needs to start with a little offset since it has to be in the middle of the filler character and i get this
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *

You haven't stated the desired outcome, but remove the /2 might do the trick.
Maybe replace both \t with a space.
Remove the '\n' from println, unless you want a blank line between lines.

How about this:
try (final Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name())) {
System.out.print("Enter the length of the side of your equilateral triangle: ");
final int side = scanner.nextInt();
System.out.print("Enter the character you want your triangle to be filled with: ");
final char character = scanner.next().charAt(0);
for (int i = 1; i <= side; i++) {
final StringBuilder builder = new StringBuilder(side);
for (int k = 0; k < (side - i); k++) {
builder.append(' ');
}
System.out.printf(builder.toString());
for (int j = 0; j < i; j++) {
System.out.printf("%s ", character);
}
System.out.println();
}
}
Please notice the try-with-resources with Scanner — only available in from Java 7 (and onwards).

Related

how to input 2 digits in a char in my case

I have this code that is based on seats but the rows exceeded to 10+. When i input 1A it will change the seat row 1 column a from '*' to 'x'. Now my problem is when it comes to 2 digits e.g. 12B, it will give me error because it only takes the '1' not the whole '12' in a char that detects the row to change it to 'x'.
I kind of have the idea on my problem, it is because it's character and follows ASCII but can't seem to find the solution to my problem. I know this is a assignment and got some of the code references from this site also, but i study them as i took some references, not just copy and paste.
This is where i get stuck
if (choice == 'c') {
input.nextLine();
String seatchoice = input.nextLine();
char[][] seats = new char [13][6];
for(i = 0; i < 13; i++) {
seats[i][0] = '*';
seats[i][1] = '*';
seats[i][2] = '*';
seats[i][3] = '*';
seats[i][4] = '*';
seats[i][5] = '*';
}
while (seatchoice.length() > 0 ) {
int row = seatchoice.charAt(0) - '1';
int col = seatchoice.charAt(1) - 'A';
if (seats[row][col] != 'x') {
seats[row][col] = 'x';
System.out.println(" ");
printSeats(seats);
}
}
}
the method printSeats
private static void printSeats(char[][] seats) {
System.out.println("\t A B C D E F");
for (int i = 0; i < seats.length; i++) {
System.out.println("Row " + (1 + i) + "\t " + seats[i][0] + " " + seats[i][1] + " " + seats[i][2] + " "
+ seats[i][3] + " " + seats[i][4] + " " + seats[i][5]);
}
}
Now, when i try inputting "10A" it will give me, Index -17 out of bounds for length 6. I'm expecting it to change the row 10 column A to be x
A B C D E F
Row 8 * * * * * *
Row 9 * * * * * *
Row 10 x * * * * *
Row 11 * * * * * *
Row 12 * * * * * *
Row 13 * * * * * *
Thank you in advance. Please correct me in any ways or if there's something that i should learn. I'm willing to accept any criticism.
Since the Letter is always in the last index you can do it this way
int row = Integer.parseInt(seatchoice.substring(0, seatchoice.length() - 1)) - 1;
int col = seatchoice.charAt(seatchoice.length() - 1) - 'A';

Not getting a half pyramid instead just getting a straight line of values in java

Hi I am new to Java and just trying out some basic programming, I just wanted to try a program where * prints in half pyramid but I end up getting in straight line. Can anybody please help me out?
public class Aestrixpyramid {
public static void print(int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j <= i; j++) {
System.out.println("* ");
}
System.out.println();
}
}
public static void main (String args[]) {
int n = 5;
print(n);
}
}
and got output like this
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
instead of
*
* *
* * *
* * * *
* * * * *
Thanks in advance!
Hi you have to change System.out.println("* ") for System.out.print("* ")
println prints the text and add a line break, if you want to append characters to the same line you can use print

What logic needs to be changed to get the star pattern

class Starr {
public static void main(String[] args) {
int res;
for(int i=1;i<=5;i++) {
for(int j=1;j<=5;j++) {
res=i+j;
if(res>=6) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
*
**
***
****
*****
Expected:
*
* *
* * *
* * * *
* * * * *
To get the above expected result i made the following changes,
{
System.out.print(" *"); /* Added a space before '*' */
}
else
{
System.out.print(" "); /* Added 2 spaces */
}
I would like to know if this expected result can be achieved in another logic where i don't have to change the print statement. Whatever changes i have done is a right approach?
You cannot achieve a way of printing whitespaces between the stars without printing anything, although you can achieve the desired output without using whitespaces. This could be done with System.out.format() or System.out.printf(). format and printf are actually the same thing in practice. For you particularly:
System.out.printf("%2s", "*");
This means that this output should print two characters, out of which the first one should be '*'. The rest will be whitespaces.
public class StarPattern {
public static void main(String[] args) {
// This loop print the number of * rows
for (int i = 5; i >= 1; i--) {
// This prints the empty space instead of *
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
// Print the * in the desired position
for (int k = 5; k >= i; k--) {
System.out.print("*");
}
// Move the caret to the next line
System.out.println();
}
}
}
Output:
*
**
***
****
*****
Check this code, It works!
int res;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
res = i + j;
String sp = (j != 1) ? " " : "";
if (res >= 6) {
System.out.print(sp + "*");
} else {
System.out.print(sp + " ");
}
}
System.out.println();
}
Output:
*
* *
* * *
* * * *
* * * * *

Printing a Checkerboard - Code compiles but not outputting what I want

My code compiles, but it's not outputting what I want.
import java.util.Scanner;
public class Checkerboard {
public static void main(String[] args) {
int num, col;
//Scanner for input
Scanner keyboard = new Scanner(System.in);
//Get input
System.out.println("Enter a number: ");
num = keyboard.nextInt();
System.out.println("Enter the same number: ");
col = keyboard.nextInt();
for (int n = 0; n < num; n++) {
for (int c = 0; c < col; c++) {
System.out.print("* ");
}
System.out.println(" ");
}
}
}
It is outputting the pattern
(if N, C = 5)
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
What I want is
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Any tips on how I could output what I want?
Insert
if(n%2==0){
System.out.print(" ");
before the inner loop.
Currently you are adding the space at the end of the line instead of at the beginning of the next line, and anyway, you only want to add the space not on each line, but only any other line.
for (int n = 0; n < num; n++) {
if (n % 2 == 0) {
System.out.print(" ");
}
for (int c = 0; c < col; c++) {
System.out.print("* ");
}
System.out.println();
}
Just change your inside for loop to
if(n%2==0){
System.out.print(" *");
} else {
System.out.print("* ");
}
Out put:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
change inside for loop:
if(n%2==0){
System.out.print(" *");
} else {
System.out.print("* ");
}

Incomplete output with while loop

I am supposed to give this output
* * * * *
* * * * *
* * * * * *
* * * * *
so on and so forth 5 itirations
but it only shows the first 2 output
here's my code
public class itiration {
public static void main( String args[]){
int counter1 = 1;
int counter2 = 1;
int counter3 = 1;
while(counter1<=5)
{
while(counter2<=5)
{
System.out.print("* ");
System.out.print(" ");
counter2++;
}
System.out.println();
while(counter3<=5)
{
System.out.print(" ");
System.out.print("* ");
counter3++;
}
System.out.println();
counter1++;
}
}
}
this is not a homework
Have you tried stepping through this program with a debugger?
HINT: After the outer loop executes its first iteration, what are the values of counter2 and counter3?
You need to reset counter2 and counter3 in the loop (after counter1++ for example), otherwise they'll stay at value 5 after the first run of the loop, and the inner loops will not run any more.
You're not resetting counter2 and counter3 for each iteration of the main loop.
Try this:
int counter1 = 1;
while(counter1<=5)
{
int counter2 = 1;
int counter3 = 1;

Categories