• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning array To Method

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys im new to Java and have been building up some code over the past week. This is actually the first program i've ever written. Looking online at some examples im having a tough time with what i believe should be something simple.

Basically i need to create a method to return an array called "arrayList" and sort it. Then i need to print that array. I believe Im almost there but am a bit unsure.


 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nick,
forget your method arrayList(), it's completely wrong, delete it.

For example you could write a method that receives a List as a parameter, performs the sort and prints it out.



 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you write Object[line] as a return type? Surely it would be Object[].

I am not convinced about the printOrderedList method either; that does two things, viz printing the List, and sorting it; the latter is a side-effect which may cause problems when the List is used later, because its contents are in different locations.
As I said yesterday, make each method do one thing and do it well.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And in that method, you cannot have code after the return statement.
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


lol i did write Object[] i corrected myself straight away. obviously you cant have an array size set when you dont know how big it is thanks tho
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicola Garofalo wrote:Hi Nick,
forget your method arrayList(), it's completely wrong, delete it.

For example you could write a method that receives a List as a parameter, performs the sort and prints it out.





If i use this method however shouldn't i be returning the array as an object rather than an ordered list of strings? I mean i'd like it to appear in that manner but doesn't java have a rule about returning an array as an object in general?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a list that can be sorted. Do it in one or two methods, as you prefer, but why should you convert it to an array?
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicola Garofalo wrote:You have a list that can be sorted. Do it in one or two methods, as you prefer, but why should you convert it to an array?



Ohh i was thinking of it as an array storing a list and not a list. In my head i was thinking
return the array to the method
sort the array
print the array

 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nick,
if you want to convert a list to an array of Objects, do it as an exercise , it's ok, but with your list in your hand you can do whatever you want to do for your purpouses, without converting anything:
sort it and print it out.

If you need two methods, one for sorting and one for printing you could write those ones, take them as an example:

1) write a method that takes a List as an argument and returns the same list sorted

2) write a method that prints out a list of Strings given as an argument

 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i've imported the extra libararys and compiled the program with no errors. Lovely

However I need to call my method so that It only prints after its read the entire document line by line. else, loop back to my find method.

regards Nick
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* Please try to normalize your indentation and make sure any comments actually communicate something useful: the "why", not the "what". For example, I already know that the "import" statement imports Java libraries.

* Comments for classes and methods, when necessary at all, should follow standard Javadoc conventions rather than using the double-slash notation.

* Please PostRealCode -- as written this code won't compile. This adds a layer of aggravation for folks who prefer to look at Java code in an IDE (so we can format it rationally :)

* Remove the infinite loop.

* All of your functionality is in the constructor--this won't "return" anything, as its comment claims. A constructor will just return a new Find--consider putting the code that actually *does* something into an instance method, then calling that method. Perhaps with a file name as an argument (it could also be a constructor argument; whichever).

Particularly when requesting help from others, it's nice to make things as easy for us as possible.

 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:* Please try to normalize your indentation and make sure any comments actually communicate something useful: the "why", not the "what". For example, I already know that the "import" statement imports Java libraries.

* Comments for classes and methods, when necessary at all, should follow standard Javadoc conventions rather than using the double-slash notation.

* Please PostRealCode -- as written this code won't compile. This adds a layer of aggravation for folks who prefer to look at Java code in an IDE (so we can format it rationally :)

* Remove the infinite loop.

* All of your functionality is in the constructor--this won't "return" anything, as its comment claims. A constructor will just return a new Find--consider putting the code that actually *does* something into an instance method, then calling that method. Perhaps with a file name as an argument (it could also be a constructor argument; whichever).

Particularly when requesting help from others, it's nice to make things as easy for us as possible.


Apologies, i try to keep my comments to one line and simplistic in my posts to save space and people the trouble of reading.

Im new to java but basically explaining in a bit more detail. I have a html file which lists several resource names the names however are listed for example as <@dyanmichtml resource_name@>, the only part of that instance i want is resource_name.

So to capture these data instances (which are always on one line) with html code below. I have a program which will read in the file line by line. I then list the identifiers in order to capture the data at the beginning, middle and end of the line.

In my program I am assigning the data stored in the middle of the program to aa string called "line". the loop reads the document until an instance of line is found and then adds it (if true) to an array list.
then we have a method to return the array list, sort it and print it.

However there is still no output for this program although I am having no compilation errors. So am i right in thinking
Would i not have to now call my newly created print method in the main method with an if statement saying If the buffer has finised reading the document then call printOrderedList(); else loop back to Find();

Or would i add that in the main and add an else to the If loop within the while saying if line = null || document is read (im not sure how to say this in java) then call printOrderedList();

regards Nick
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Nick, you solved the first part of your problem. You now have the ArrayList, the code compiles and we saw it yesterday that it works.
Now you just have to sort this ArrayList and print it out.

Don't think about the file operations. They are over, your parsed lines are in an ArrayList, the reader has finished its operations and it is closed now.

Just add 2 rows to your code and your work is over.



No more things to do. Be sure that data are printed out in the desired order. Then we will talk about splitting code into different methods.
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just add 2 rows to your code and your work is over.


.

Im a little confused now, where am i executing the above mentioned code?
Because we have the printOrderedList method which is returning the list as an integer. I then thought it was sorting and printing out the list.

So why am I adding the above code referencing the arrayList object?
This may be a silly question but i just want to understand things as best I can.

regards & thanks

Nick
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Rowe wrote:Apologies, i try to keep my comments to one line and simplistic in my posts to save space and people the trouble of reading.


What I'm saying is that the comments in the code weren't useful, and because of their strange indentation were even more distracting than usual.

It wasn't a request for more explanation--we all know what you're trying to do. It was a request to keep things as clean and readable as possible, along with the other comments.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing is silly,
you are going away form the solution, that is in front of you.


printOrderedList method which is returning the list as an integer


Where in this discussion someone said that?

Anyway:

after the line

your arraylist will be sorted.

After

Your arrayList will be printed on the output.

Why don't you try to do it?

Add the above 2 lines after the while loop, like the following:


 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way to put what David Newton said: Get all your code into a format and quality which you would hand in for your assignment, or show to your boss as production code, then it will be easier for us to read and test. Comments about methods should be proper documentation comments /** . . . */, for example.
And "quality" includes correct indentation.
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
apologies i put integer instead of parameter, Nope still no output. Though from looking at the example you gave wont that if it did work, print out every time an instance of line is found?

I need the array to build up instance by instance of line (IF) there is an instance of a resource to be captured on a line then capture it, however this is not always the case, so there isnt an instance on each line of the document.
I only want to sort and print the array once the resource names have all been collected from the document. until then i just want to keep it looping until the document is read.

regards Nick
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example i asked you to test, you print the arrayList, the entire one, outside the loop, after it, once the html file is completely parsed line by line.
The method find filters all the lines. If there's no line that matches your pattern your ArrayList may be empty, thus your output could be empty.
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicola Garofalo wrote:In the example i asked you to test, you print the arrayList, the entire one, outside the loop, after it, once the html file is completely parsed line by line.
The method find filters all the lines. If there's no line that matches your pattern your ArrayList may be empty, thus your output could be empty.



There are 3 instances of resources within the document itself in this case and the pattern matches any data caught between <@dynamichtml and @> This is always the case the data does not change, the instance will always be between these two string values. As such there should be three results.

regards Nick

 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
delete line 22, it's useless since you sorted and printed the arrayList just two rows before.

The rest is ok, no reasons not to work if the html file is well formed.
May i see the html lines that contains the pattern searched?

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems to be improving, but
  • You ought not put executing code in a constructor, only code to set up the object's initial state and invariants.
  • You have no access to the List<String> from anywhere else.
  •  
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Since i am not calling the printOrderedList I should safely be able to remove it?

    Below is a copy of an instance from the html file as requested

     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, remove it. I guarantee, nothing bad is going to happen

    I get the following output if i save your code in a file called my.html placed in a folder of my filesystem, and if i execute your application.



    The output,as expected, is made of one element, because one is the line that matches your pattern.

     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Nicola Garofalo wrote:Yes, remove it. I guarantee, nothing bad is going to happen

    I get the following output if i save your code in a file called my.html placed in a folder of my filesystem, and if i execute your application.



    The output,as expected, is made of one element, because one is the line that matches your pattern.



    Hi Nicola,

    Yeh i figured since it wasn't being executed it would be safe to remove, thankyou
    I'm confused though my html file is stored in the aformentioned route and when i use Dos and do a javac Find.java nothing happens it just compiles and has no errors. Am i missing/doing something wrong?

    P.s. thankyou sososososososososososoosososososoooooooooooo much for your help, you've been an absolute angel and really really helped me out on a mahusive scale with this xxxxxxxxxx
     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh, really no problem Nick.

    Well, once the file is compiled you should execute the program. You are missing the funny part

    Now find in your file system the file Find.class generated by the execution of your javac command
    As you wrote the class it should be in the same folder of your Find.java file

    then execute the command



    see what happens and let me know

     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Nicola Garofalo wrote:Oh, really no problem Nick.

    Well, once the file is compiled you should execute the program. You are missing the funny part

    Now find in your file system the file Find.class generated by the execution of your javac command
    As you wrote the class it should be in the same folder of your Find.java file

    then execute the command



    see what happens and let me know




    Ok So i've checked and Find.class is in the same folder as generated by the compiler.
    I then drilled down to the directory via Dos and exected the compiler as normal via the javac Find.java comm and then attempted to execute the Find.java and get exception to thread "main " java.lang.NoClassDefFoundError: Find

    caused by a series of at java.net.URLClassLoader$1.run<Unknown Source> plus various others.

    regards Nick
     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not




    But



    Exactly as i have written it
     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Nicola Garofalo wrote:Not




    But



    Exactly as i have written it



    Apologies, the previous message had a typo lol java Find is what i entered
    regards Nick
     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    java Find is the command I used to execute the program after the normal javac command. Once i executed the java Find command the previously mentioned errors popped up.



    I'll post my code up again.

     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Go to the the directory where your Find.class file is.

    Then type

    java -cp . Find
     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Nicola Garofalo wrote:Go to the the directory where your Find.class file is.

    Then type

    java -cp . Find



    Is the above command correct? if i type java -cp.Find? When i enter this I get unrecognised option.
    However if I type java-cp.Find i get an error list as previously saying

    could not find the main class cp.Find program will exit?
    regards Nick
     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    type java then space the -cp then space the . then space then Find

     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Nicola Garofalo wrote:

    type java then space the -cp then space the . then space then Find



    YEAAAAAAH! that worked, thank you

    Sorry to be inquisitive but I am curios as to why this is, for example;
    Does my program always have to be executed this way?
    Why doen't it execute with the java Find command???
    Is their anything I need to do because of this issue?
    Also what does the command "java -cp . Find" do, i get that javac is the compiler and java Find executes the program so what does java -cp . do, search for something and why do we have to search for it?

    regards Nick

     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    It's a classpath issue.
    Java has to know where to find resources

    if i type



    i tell java that the classpath is the current directory (.)

    To avoid to specify it every time you invke the command you can set the environment variable CLASSPATH in your system, for example

    Look at this link for detailed information

     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Haha that was such and awesome little icon

    Thanks, i've successlly changed the class path via the control panel and Dos seems happy now.
    However, lol. The html file I read was a cut down version, which had only 3 resources in, this worked fine.
    When using the full html file however Its printed out the resources but not in alphabetical order like before. Is there any reason for this, ill attach the html file

     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, that's another issue.

    The ordering on String is not alphabetical but an Unicode ordering.

    So letters from A to Z goes from 65 to 90

    Letters from a to z from 97 to 122

    Then every uppercase letter will come first than a lowercase letter. Zorro is "less than" batman

    You have 2 choices dependng on how important is for you the Upper - Lower case thing.

    1) If you don't care about the letter's case you put all your lines in the List in lower case or upper case. Then the sort will be performed as you expected.
    2) If you do care you write a class that implements a Comparator, and pass it to the Collections.sort method, so that when the List will be sorted it will be sorted with your criteria

     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is a case-insensitive Comparator<String> as a field of the String class.
     
    Nick Rowe
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi guys apologies about not posting for the past few days my sister was in a car accident over the weekend and i've spent pretty much all of my time at the hospital with her.

    Anyway, my program now organises and prints each resource name alphabetically on a seperate line ( which is nice) , however the program organises the resources in a case sensitive mannor and not in a general way. For example Timlinemanager_alpha may be listed infront of searchapi_footer purely because it begins with a capital T where as it should be listed before.

    I have been looking into comparator interfaces to try and do something along the lines of A==a,B==b etc but am not really sure how to go about it.

    The code I now have I tidied up after discovering Eclipse the other day.

    regards Nick

     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey Nick,
    hope everything is ok with your sister now.

    Anyway, there wasn't much left to do

    just replace



    with



    as suggested above.
    Good luck.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic