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
1,140 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 22, 2019 by Utsab Bandyopadhyay

Related Questions In Java

+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 81,222 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,140 points
1,542 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
839 views
0 votes
4 answers

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

You can use DecimalFormat. One way to use ...READ MORE

answered Dec 16, 2020 in Java by Gitika
• 65,770 points
164,470 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,140 points
764 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
• 58,020 points
1,225 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 3,660 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
846 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
• 59,230 points
2,310 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP