This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Array in Method Two Questions? Any one know how to do them? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array in Method Two Questions? Any one know how to do them?" Watch "Array in Method Two Questions? Any one know how to do them?" New topic
Author

Array in Method Two Questions? Any one know how to do them?

Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
5. Create a method that will open your sports csv file for reading, load the file into a 5xX array. The method will then return the
loaded array to the main program. 5xX: The five represents the number of columns (fixed). The “X” represents the number
of data rows (variable). Your program should be able to handle not only your data file, but also your neighbors’.
Signature: public static String[][] loadSport(String strFileName)
6. Create a method that will take a two dimensional array of data (like your sports array) and print it to the screen.
Signature: public static void printSport(String[][] strSportArray)
//Question5

Firstly, I don't know how to return the array I made, any one know?
Secondly, I don't know how to print it out using a method with the signature in question 6
Any one know how to make these two method?
I am using the old hsa library so it may looks weird but can any one just tell me the process behind it?
Writing it out for me would also help a lots! Thousand Thanks!

added code tags. Read UseCodeTags
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanpeter wong wrote:
Firstly, I don't know how to return the array I made, any one know?


You return an array the same way you return anything else. With a return statement.

Secondly, I don't know how to print it out using a method with the signature in question 6


What have you tried so far and what specific problems are you having?

Writing it out for me would also help a lots!


No, actually, doing your work for you would not help at all. This site is NotACodeMill,(⇐click) and you'll learn more by doing the work yourself, with gentle nudges in the right direction when you get stuck, of course.

And welcome to the Ranch!
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
Ok! I first tried to write return strline2;
but when i use this thing and input it into another array like
String strline[][];
strline=xutils.loadsport("x.csv");
I don't know how to print out the array "strline"

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanpeter wong wrote:Ok! I first tried to write return strline2;
but when i use this thing and input it into another array like
String strline[][];
strline=xutils.loadsport("x.csv");
I don't know how to print out the array "strline"


You need to iterate over the array, printing out each element as you go. Or, if you're allowed to (which I kind of doubt, given the nature of this exercise), you could just call java.util.Arrays.deepToString(strline).
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
Jeff Verdegan wrote:
Stanpeter wong wrote:Ok! I first tried to write return strline2;
but when i use this thing and input it into another array like
String strline[][];
strline=xutils.loadsport("x.csv");
I don't know how to print out the array "strline"


You need to iterate over the array, printing out each element as you go. Or, if you're allowed to (which I kind of doubt, given the nature of this exercise), you could just call java.util.Arrays.deepToString(strline).


But you haven't teach me how to print out the array on to the screen
Also, the method must be separate, meaning I can't just print out the stuff as I go along unless I know the size
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

Stanpeter wong wrote:
Jeff Verdegan wrote:
You need to iterate over the array, printing out each element as you go. Or, if you're allowed to (which I kind of doubt, given the nature of this exercise), you could just call java.util.Arrays.deepToString(strline).


But you haven't teach me how to print out the array on to the screen


Teaching is about giving you hints in the right direction -- "iterate over the array, printing out each element as you go" is good enough to get you started. Try to implement it. You won't learn if you expect answers without trying.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanpeter wong wrote:
But you haven't teach me how to print out the array on to the screen


Yes I did. I told you that you iterate over the array, and print each element as you go.

So what part of that do you not understand?

I think you understand how to iterate over an array, since you're doing it in the code you posted. So do you not understand how to print something? I'm sure you must have used System.out.println() at some point.

You should google for java array tutorial for more details and examples. make an effort, and if you get stuck, show what you've tried and indicate clearly exactly what problems you had.

Also, the method are separated, I can't jsut print out the stuff as I go along


Yes, you can. Although you don't have to do it that way.
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
Henry Wong wrote:
Stanpeter wong wrote:
Jeff Verdegan wrote:
You need to iterate over the array, printing out each element as you go. Or, if you're allowed to (which I kind of doubt, given the nature of this exercise), you could just call java.util.Arrays.deepToString(strline).


But you haven't teach me how to print out the array on to the screen


Teaching is about giving you hints in the right direction -- "iterate over the array, printing out each element as you go" is good enough to get you started. Try to implement it. You won't learn if you expect answers without trying.

Henry

<3 I will try my best
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
strline2=new String [intline][5];
infile.close();
infile=new TextInputFile(strFileName, Console con);
for(intcounter=0;intcounter<intline;intcounter++){
strrandom=infile.readLine();
strline=strrandom.split(",");
strline2[intcounter][0]=strline[0];
con.println(strline2[intcounter][0]);
strline2[intcounter][1]=strline[1];
strline2[intcounter][2]=strline[2];
strline2[intcounter][3]=strline[3];
strline2[intcounter][4]=strline[4];
}
= = I tried to put console con to print out the stuff as I go but it just don't work out
public static void printSport(String[][] strSportArray, Console con){
//eg: int intcount;;
for(intcount=0;intcount<I DON"T KNOW WHAT IS THE SIZE; intcount++){
con,println(strSportArray[intcount][0]+strSportArray[intcount][1]+strSportArray[intcount][2]+strSportArray[intcount][3]+strSportArray[intcount][3]+strSportArray[intcount][4]+strSportArray[intcount][5]);
}
}
for this I seriously have no idea how to get the size of the array to print out each one of them
I have no idea how to print the line without knowing the sizes of the array
IT JUST DOESN"T WORK !!!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

When posting code, UseCodeTags(⇐click) so it will be readable.

Stanpeter wong wrote:
= = I tried to put console con to print out the stuff as I go but it just don't work out


Since I can't see that actual code, and since you didn't explain what "just doesn't work out" means, there's no way I can help you there.

I DON"T KNOW WHAT IS THE SIZE


Yes, you do. If you don't remember how to get an array's size, look at any java array tutorial.

con,println(strSportArray[intcount][0]+strSportArray[intcount][1]+strSportArray[intcount][2]+strSportArray[intcount][3]+strSportArray[intcount][3]+strSportArray[intcount][4]+strSportArray[intcount][5]);


Since you have a 2D array (really an array of arrays) you could also use a nested loop.

IT JUST DOESN"T WORK !!!


Note that ItDoesntWorkIsUseless.(⇐click) You need to TellTheDetails(⇐click) of exactly what's going wrong. I think I've mentioned this already. I request that you pay closer attention, and spend some time doing some research as I have now suggested at least twice.

Keep at it, and good luck!
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
lets assume this
int x=int [10][10] //Since I know the size I can print it out with a for loop
for(intcounter2=0;intcounter<10;intcounter++){
for(intcounter=0;incounter<10;intcounter++){
con.println(x[intcounter2][intcounter]); // and it will output just fine
}
}
Now not knowing the size and assume
intx is a 2D array
how to put it in a for loop or any where to print it out?
Without any limitation, it may only goes up to 2, or maybe 1
How can I know the size of the array;
and I read the tutorial, they only show you how to out put a array with known size
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

Stanpeter wong wrote:
Now not knowing the size and assume
intx is a 2D array
how to put it in a for loop or any where to print it out?
Without any limitation, it may only goes up to 2, or maybe 1
How can I know the size of the array;


Take a look at the link provided by Jeff again. If it isn't yet obvious from the many responses -- given any array, you can get the length.

Henry
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html < I read this
http://www.roseindia.net/java/beginners/arrayexamples/index.shtml <I read this
http://www.learn-java-tutorial.com/Java-Arrays.cfm <I read this
All of them have initialize the array with known size
But my question is that how to print out a array without knowing it sizes
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

Stanpeter wong wrote:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html < I read this
http://www.roseindia.net/java/beginners/arrayexamples/index.shtml <I read this
http://www.learn-java-tutorial.com/Java-Arrays.cfm <I read this
All of them have initialize the array with known size
But my question is that how to print out a array without knowing it sizes


You can get the size of any array from the "length" field.

Henry
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
String strline[][];
strline=new String [5][5];
strline[0][0]="A";
strline[0][1]="B";
strline[1][2]="C";
int sum = strline.length;
con.println(sum);
//Output is zero

Although it have something in the array why does the .length just return zero?
Also, array.length is for 1D now 2D you can't get 2D with that
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanpeter wong wrote:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html < I read this
http://www.roseindia.net/java/beginners/arrayexamples/index.shtml <I read this
http://www.learn-java-tutorial.com/Java-Arrays.cfm <I read this
All of them have initialize the array with known size
But my question is that how to print out a array without knowing it sizes


And all of them mention the length attribute. You don't seem to be putting much effort into this, and you're still not using code tags. I'm done here. Good luck.

One final thing: Never, ever use RoseIndia. It's a horrible site with lots of errors. Just avoid it altogether.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanpeter wong wrote:String strline[][];
strline=new String [5][5];
strline[0][0]="A";
strline[0][1]="B";
strline[1][2]="C";
int sum = strline.length;
con.println(sum);
//Output is zero

Although it have something in the array why does the .length just return zero?


It doesn't. If that's printing zero, then there's something wrong with the con.println() method. I absolutely, 100% guarnatee you that strline.length is not zero there.
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
It's a 2D array not a 1D array, I returns zero because .length can't do 2D!!! So now, is there command that exist in this world that can find the szie of 2D array?

String strline[] = new String [5];
int sum = strline.length;
con.println(sum); //Output Sum=5

String strline[][]=new String[5][5];
int sum=strline.length;
con.println(sum); //Output Sum=5

but is that the x or the y??
Never mind = = it's the x
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanpeter wong wrote:It's a 2D array not a 1D array, I returns zero because .length can't do 2D!!!


False.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

Stanpeter wong wrote:It's a 2D array not a 1D array, I returns zero because .length can't do 2D!!! So now, is there command that exist in this world that can find the szie of 2D array?

String strline[] = new String [5];
int sum = strline.length;
con.println(sum); //Output Sum=5

String strline[][]=new String[5][5];
int sum=strline.length;
con.println(sum); //Output Sum=5

but is that the x or the y??
Never mind = = it's the x


It's going to be tough trying to figure it out yourself. I highly recommend going back to the documentation and trying to read them again.

Basically, Java doesn't directly support 2D arrays. It supports arrays of arrays -- which is almost the same thing. The length field of any array of arrays is, of course, the outer array. To figure out the lengths of the array elements, you need to use the length fields of the element arrays.

Henry
Stanpeter wong
Greenhorn

Joined: Feb 11, 2012
Posts: 11
Henry Wong wrote:
Stanpeter wong wrote:It's a 2D array not a 1D array, I returns zero because .length can't do 2D!!! So now, is there command that exist in this world that can find the szie of 2D array?

String strline[] = new String [5];
int sum = strline.length;
con.println(sum); //Output Sum=5

String strline[][]=new String[5][5];
int sum=strline.length;
con.println(sum); //Output Sum=5

but is that the x or the y??
Never mind = = it's the x


It's going to be tough trying to figure it out yourself. I highly recommend going back to the documentation and trying to read them again.

Basically, Java doesn't directly support 2D arrays. It supports arrays of arrays -- which is almost the same thing. The length field of any array of arrays is, of course, the outer array. To figure out the lengths of the array elements, you need to use the length fields of the element arrays.

Henry


Thanks to you, problems solved ! I don't really know why can't you tell me directly .length exist in this world but thousand thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

Jeff Verdegan,

Your post was moved to a new topic.


EDIT: The reason it was split was because I want to start a topic about private messages. In retrospect, I should have made a copy of it instead. Here is the content from the split off post...


Jeff Verdegan wrote:
Stanpeter wong wrote:I don't really know why can't you tell me directly .length exist in this world but thousand thanks!


As I told you in response to the rude PM you sent me, that isn't a good way to learn, and every intro to Java arrays, including the ones you claim to have read, covers it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

Stanpeter wong wrote:
Thanks to you, problems solved ! I don't really know why can't you tell me directly .length exist in this world but thousand thanks!


Yeah... That's how we do it here on the ranch. It does take longer initially, but (a) you learn how to research it yourself, (b) hopefully, you would have learned other things while you was researching it. It's actually a great habit to get into, as when you become a professional programmer, the problems won't be so easy that you can get the answer with a single question on a forum.... It's the "teach how to fish" versus "giving a fish" argument.

Henry

 
Consider Paul's rocket mass heater.
 
subject: Array in Method Two Questions? Any one know how to do them?
 
Similar Threads
Reference Specific Line in Text File
oldskool linked list
Arrays with files
can not open the file
Confused with file read