Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java loops

  • 06-06-2011 05:21PM
    #1
    Closed Accounts Posts: 3,915 ✭✭✭


    Hi guys. Having trouble with this very simple program icon11.gif. I copied the code exact from the book but I get different results. Anyone know where I went wrong and can point me towards a fix ?



    public class DisplayStars
    {
    public static void main(String[] args)
    {
    int num;
    System.out.println("Size of squares?");
    num = EasyIn.getInt();
    for (int i = 1; i <= num; i++)
    {
    for (int j = 1; j <= num; j++)
    {
    System.out.println("*");
    }
    System.out.println();
    }
    EasyIn.pause();
    }
    }


    Its suppose to print a square of stars but prints them all on different lines.

    Instead of
    ***
    ***
    ***


    I get
    *
    *
    *

    *
    *
    *

    *
    *
    *


Comments

  • Registered Users, Registered Users 2 Posts: 196 ✭✭dumb_parade


    public class DisplayStars
    {
    public static void main(String[] args)
    {
    int num;
    System.out.println("Size of squares?");
    num = EasyIn.getInt();
    for (int i = 1; i <= num; i++)
    {
    for (int j = 1; j <= num; j++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    EasyIn.pause();
    }
    }

    See the change in bold, println writes a new line everytime, print just appends onto the current line


  • Registered Users, Registered Users 2 Posts: 214 ✭✭KMFCross


    The println("*") is the problem it should be print("*"), the println is appending a new line each time.

    Oops did'nt see dumb_parades reply.


  • Closed Accounts Posts: 3,915 ✭✭✭MungBean


    public class DisplayStars
    {
    public static void main(String[] args)
    {
    int num;
    System.out.println("Size of squares?");
    num = EasyIn.getInt();
    for (int i = 1; i <= num; i++)
    {
    for (int j = 1; j <= num; j++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    EasyIn.pause();
    }
    }

    See the change in bold, println writes a new line everytime, print just appends onto the current line


    Ahh thanks. I feel a bit thick for not spotting that, just so used to writing println. Thanks for the help and the quick response icon7.gif


  • Closed Accounts Posts: 3,915 ✭✭✭MungBean


    See next post.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Gah please use code tags :(


  • Advertisement
  • Closed Accounts Posts: 3,915 ✭✭✭MungBean


    Another question on loops. This time the do...while loop.

    In the end test
    }while (input == 'y');
    
    How can I add another option for the test to be true ? for instance if the user put Y instead of y but still work ?

    Do I have to add a second loop ? or can I alter what I have?
    }while (input == 'y' || 'Y')
    
    something like that ?


    You could have answered the question while you were complaining :(


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    You're almost right...
    while(input == 'Y' || input == 'y')
    

    Or just turn convert input to lowercase blindly if case doesn't matter!


  • Closed Accounts Posts: 3,915 ✭✭✭MungBean


    fasty wrote: »
    You're almost right...
    while(input == 'Y' || input == 'y')
    
    Or just turn convert input to lowercase blindly if case doesn't matter!

    Thanks :) been fiddlin with that for ages.


  • Registered Users, Registered Users 2 Posts: 177 ✭✭joebloggs123


    what about
    while("y".equalsIgnoreCase(input))
    


  • Closed Accounts Posts: 3,915 ✭✭✭MungBean


    what about
    while("y".equalsIgnoreCase(input))
    


    Got an error when trying to compile.


  • Advertisement
  • Closed Accounts Posts: 9,139 ✭✭✭-Trek-


    import java.util.*;
    
    public class WhileLoop{
            public static void main(String args[])
            {
    
                    String userInput  = "";
                    Scanner input = new Scanner(System.in);
    
                    System.out.print("Do you wish to continue? y/n:  ");
                    userInput = input.nextLine();
    
                    while(userInput.equalsIgnoreCase("y"))
                            {
                                    System.out.println("Continuing ...");
    
                                    System.out.print("Do you wish to continue? y/n: ");
                                     userInput = input.nextLine();
    
                            }
            }
    
    
    }
    
    
    


Advertisement