Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java loops

  • 06-06-2011 4: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: 195 ✭✭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