how to print this statement in java print(' '*(r-x-1)+'*'*(2*x+1)) ( logic used in python .its perfect) but java wrong output came

+1 vote
Dec 5, 2019 in Java by Azlan
• 130 points
174 views

1 answer to this question.

0 votes

In ' '*(r-x-1) and '*'*(2*x+1), you used multiplication of strings, which python supports but java does not. The problem can be solved by writing a loop which adds to the string on every iteration.

String r=""; //creating a String variable to store the string to be printed

for (int i=1; i<=(r-x-1); ++i) { //loop will repeat (r-x-1) times

    r=r+" "; //adding ' ' to r every time the loop runs

}

for (int i=1; i<=(2*x+1) { //loop will run (2*x+1) times

    r=r+"*"; //adding '*' to r every time the loop runs

}

System.out.println(r); //printing r

Hope this helps. :) Lemme know if this solved your problem, k?

answered Dec 21, 2019 by Utsab Bandyopadhyay

Related Questions In Java

+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,400 points

edited Nov 6, 2018 by Omkar 13,156 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,080 points
72 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
113 views
0 votes
2 answers

How can we display an output of float data with 2 decimal places in java? Please help

Use String.format()  if you need the value as ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 46,080 points
86 views
0 votes
1 answer

How to use pairs or 2-tuples in java?

You can refer the following code: public class ...READ MORE

answered Jun 7, 2018 in Java by Daisy
• 8,080 points
73 views
0 votes
1 answer
+4 votes
1 answer

How to print the individual occurance of elements in Java?

You can use a HashMap to serve ...READ MORE

answered Dec 4, 2018 in Java by Priyaj
• 57,510 points
136 views
0 votes
0 answers

How to run python script in java

Aug 22, 2019 in Java by anonymous

closed Aug 22, 2019 by Omkar 219 views
+1 vote
0 answers

output occurs but invisible immediately how to rectify this ?

public class evenodd { public static void main(String[] ...READ MORE

Oct 17, 2019 in Java by BIPLAB
• 130 points
168 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 46,080 points
256 views