Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A Java method for reading rows and columns?

  1. Feb 26, 2010 #1
    I have an assignment where we have to use loops to print quarter diamond patterns.

    I don't really need help with the actual coding, since the prof basically gave us a pseudo-code as a hint, but in the pseudo-code there is a variable for the number of rows and columns.

    I have been searching the Java API but its hard to wade through without having a better sense of what you are looking for.

    So my big question is:

    Is there a method that lets you read the number of rows and columns from your previous OUTPUT?
  2. jcsd
  3. Feb 26, 2010 #2
    plz specify your question,,,r u gonna take the number of rows and columns from the user?
    if not ,what do u mean by previous output
  4. Feb 26, 2010 #3
    Yes, in hindsight I was a little vague.

    The pattern we have to make is like this:
    continuing to a specific number of stars input by the user.
    But the professor wants us to use only a single print statement, while creating the rest of the pattern by looping it.

    But the way he seems to be implying we do it requires that you know how many stars have already been printed. He refers to this variable as num-row, or num-column.
    I am just confused how I could get these values because I am not aware of a method like that. I need them to tell the code when to stop running (i.e. when the number of rows equals the width specified by the user).

    Thanks for any help, if this makes sense...
  5. Feb 26, 2010 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You already had these values. If you need them again, then don't forget them. (You needed them to print the previous row, right?)
  6. Feb 26, 2010 #5
    I think it would be better if I phrased the question outside of the context of my professor's hints.

    I guess what I am really confused on is how to quantify the number of stars while still having a single print statement.

    In order to use a loop I need a condition to be met that uses numbers. I cannot think of a way to have a variable that represents the number of stars being printed, and therefore I don't have a way of checking if certain conditions are being met (like the width equaling the user's input.)

    haha sorry if this is not making sense. I actually am becoming increasingly unaware as to how this can be done....
  7. Feb 26, 2010 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Can you describe part of the program you need to write in Java, with the rest of it in English?
  8. Feb 26, 2010 #7
    Okay. Well I just typed a whole big thing about what I thought was workable pseudocode for this but I quickly realized that when I went through and followed the directions I was giving the computer, I was not getting the pattern. So, yeah, I need to start from scratch and just see how I want the logic to go with this.

    But thanks for making me try it in English. I should probably be able to explain it to myself before I can hope to explain it to a computer :P.

    I am very new to programming and I don't really have a brain for it I suppose.
  9. Feb 26, 2010 #8


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Following directions is difficult: giving directions even more so. Not because they are inherently difficult, but because we spend most of our lives being quite vague about things.

    This is probably the first time you are exposed to the problem of writing clear and precise directions, and the computer is very unforgiving -- it will do exactly what you tell it to do, and it will not fill in any little details you forgot to specify.

    e.g. it won't "just know" how to remember where it is in the diagram -- you have to explicitly tell it how to remember.
  10. Feb 26, 2010 #9
    public void Main(String [] args)

    int num = 0;


    num = 45;
    System.out.println("num is currently 45");


    System.out.println("What was the num value?");
    System.out.println("value is "+num);//45

  11. Feb 26, 2010 #10
    Thanks rootx but I meant reading the value as in the number of characters or something....um?

    I'm not sure. I was just being silly. Regardless.

    I realized that I can declare column and row integers in the first part of a for statement then add one to them after each iteration.

    I was seriously overcomplicating it, because I was trying to actually monitor the value of the columns and rows as they grew but I can just keep them in the loops.

    I'm not sure if that makes sense but it feel like I can make it work.

    If I have more trouble I will come back, but, thanks!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook