A Java method for reading rows and columns?

In summary, the user needs to input a number that tells the program how many rows and columns they want to print. The code will then create a pattern by looping, and inside the loop the user can input a number that tells the program how many stars to print.
  • #1
Redd
47
0
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 let's you read the number of rows and columns from your previous OUTPUT?
 
Technology news on Phys.org
  • #2
please specify your question,,,r u going to take the number of rows and columns from the user?
if not ,what do u mean by previous output
 
  • #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...
 
  • #4
Redd said:
I am just confused how I could get these values
You already had these values. If you need them again, then don't forget them. (You needed them to print the previous row, right?)
 
  • #5
Hurkyl said:
You already had these values. If you need them again, then don't forget them. (You needed them to print the previous row, right?)

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...
 
  • #6
Can you describe part of the program you need to write in Java, with the rest of it in English?
 
  • #7
Hurkyl said:
Can you describe part of the program you need to write in Java, with the rest of it in English?

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.
 
  • #8
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.
 
  • #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

..
}
 
  • #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!
 

1. How do I read rows and columns using a Java method?

To read rows and columns using a Java method, you will need to use a combination of loops and array manipulation. First, you will need to create a two-dimensional array to store the data. Then, use a for loop to iterate through the rows and another for loop nested inside to iterate through the columns. Within the inner loop, use the array indexes to access and read the data from each row and column.

2. What is the syntax for creating a Java method to read rows and columns?

The syntax for creating a Java method to read rows and columns is as follows:

public static void readRowsAndColumns (int[][] array) {

// code to read rows and columns

}

This is a basic method declaration, with the keyword "public" indicating that the method can be accessed from other classes, "static" meaning it can be called without creating an instance of the class, "void" meaning it does not return a value, and "int[][] array" being the parameter that represents the two-dimensional array to be read.

3. Can I use a Java method to read rows and columns from a CSV file?

Yes, you can use a Java method to read rows and columns from a CSV file. You will need to use a BufferedReader to read the file, then parse the data using the split() method and store it in a two-dimensional array. You can then use the same method described in question 1 to read the rows and columns from the array.

4. Is there a built-in Java method for reading rows and columns from a database?

Yes, there is a built-in Java method for reading rows and columns from a database. You can use the ResultSet class to retrieve data from a database query and store it in a two-dimensional array. You can then use the same method described in question 1 to read the rows and columns from the array.

5. How can I handle errors when using a Java method to read rows and columns?

To handle errors when using a Java method to read rows and columns, you can use try-catch blocks to catch any exceptions that may occur during the execution of the method. You can also use the throws keyword in the method declaration to specify which exceptions may be thrown, allowing the caller of the method to handle them accordingly.

Similar threads

  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top