• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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
Hey Nicola,

Thanks for that, I thought that i was going to have to create another method to deal with it. Glad it wasn't.
My sisters rli not well, she didnt break anything but the docs did some tests on here and have noticed that shes got alot of bacteria in her bloody and some muscles damaged. Her bacteria count is supposed to be 8 and is sitting on average at around 113. She can barely walk so something is really wrong.
Hopefully after the scans etc come back they'll be able to sort things.

Thinking about something else entirely, is it easy to save the output from the program into a new file?
Hope your weekend was better at least the suns out today and we have some nice white clouds, its been horrible the last couple of days.
regards nick
 
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 as easy as reading it.

Here are some steps to follow, you will put code in to make things work

  • 1)
  • build a method that receives the List and for example the file name




  • 2)

  • Build a new File with the given filename String (look API documentation for File constructors)

  • 3)

  • Create a PrintWriter around your File

  • 4)

  • use PrintWriter methods to write your List elements (line by line if you need it)

  • 5)

  • Finally flush and close the PrintWriter.

    Let's see how you do this method. Enjoy
     
    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
    Thanks Nicola,
    the examples im finding aren't that great, I've found an example that implements a try/catch method like in my program would i not use something along the lines of the example below, or is a method a more straight forward way?

    So far I've just imported the necessary librarys and created a method, the other examples i found from the sun site weren't that helpful.
    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
    Your examples copies the content of one file into another file.
    If that is what you want to do the example is right.

    But if you want to put the elements of the ArrayList that you calculated in the previous steps in a file, just iterate through your ArrayList instead of through the lines of a BufferedReader, which, in that case, you could even delete.

     
    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
    So for example could I create a method like this below and then call it within the main body somewhere?



     
    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
    I don't understand your while condition at line 8.
    Pass the ArrayList as an argument to your method, instead of an array of Strings. I wrote the signature of the method you need



    With your second argument you can pass to your method the file name to write into.

    Then you should iterate through the ArrayList. You write line by line your file


     
    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 all,

    So i've given my method a go and still having trouble but anyway heres what I have.
    Also i realise that I still need to call my method on the command line
    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
    Hi Nick,
    you miss the import statement for PrintWriter

    Whe you added it, call the method writeResults passing to it the ArrayList arrayList and a filename you chose to write to.
    For example you could call that method at line 45 after the call to sortAndPrint method.

    If you still have troubles, post the stack trace printed at runtime or the compile time error.
     
    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 done that, thanks Nicola

    However when compiling the program i now get a compilation error which pops up when i call the writeResults method.

     
    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 easy to see. Look at the signature of the method and how you called it.
    Anyway pay attention on what you do, you are not thinking about the problem, you are following instructions mechanically.
    I told you that you must pass the filename to the writeResult method. Why don't you do it? Where do you expect the arraylist is written?
     
    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
    Morning Nicola,

    I hope you had a nice weekend, although today seems to be a fight against the rain by the looks of it

    Looking at the signature of the method, its called writeResults, which is what i've called, reading in the parameters of the filename and arraylist.
    The way my mind was working was that I thought i would like to call in the arrayList to return and capture it and also the filename.
    At first i simply used the "filename" as the parameter called but this also returned an error.
    I realise this type of method is a private method, the same as the sortAndPrint which is called.

    regards Nick x
     
    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
    The signature of writeResults method is



    Look at how you call it:



    if you say that writeResult takes two arguments, why do you want to call it with just one? You can't. It won't work.

    Look at the compiler message, it's explaining what you wanted to do (and you can't do).

     
    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 im completely with you, i've sorted that out, thank you.

    Now when the program expires it only throws out two errors,


    The first expecting a ) after String and the second declaring an illegal start of an expressiong because of the closing bracket at the end of the method call.

    writeResults(ArrayList<String> arrayList, String filename);

    Why would the compiler be expecting a closing bracket, and then the removal of another when the methods signature is the same? surely anything other than the correct name would throw an error?

    I think the error in the compiler is simply suggesting where i should close the call but why would it do that knowing that it would be wrong?

    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
    Are you saying there are still compilation errors?

    If so, it'd be so much easier if you'd post the errors and the lines they correspond to.
     
    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 cannot call a method in that way



    I have already told you that some posts ago. That's one of your first mistakes. Please go back and find 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
    The error that comes up is on the line calling the writeResults method and details of the compiler error output are in the previous post with the string line attached.

    Continuing with Nicolas previous post.
    I have looked back and can see no error in my method call.

    For example the method below contains the correct signature, its also executed with a semi-colon and contains both opening and closing brackets.

    Unless you are suggesting to simply call the two parameters by using
    writeResults(arrayList, filename);

    This I have done, which only gives out the one error declaring that the compiler cannot find the string filename.

    regards 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
    There's no variable called "filename"--that's why it can't find it. You need to *provide* a filename, either in an immediate string, or in a variable.

    And you can't just post a hundred lines of code and say "look at the line where writeResults is called". Well, you *can*, but it'd be much easier if you just posted the exact error message, and the line of code that it happens on. Why make it difficult for people to help you?
     
    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

    The error is called at line 50 which simply states that it cannot find the variable name filename.
    however..
    filename is a parameter of the method I am calling, aswel as arrayList. I simply left it the string name as filename for testing purposes.

    The method below shows


    regards 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
    You may have a fundamental misunderstanding about how Java methods work.

    The filename you show *there*, in the method definition, defines the parameter *within the method*. Not outside of it. If you *call* the method you must provide an argument--an actual value. The value may be immediate ("foo.txt") or a variable within the *current* scope. Simply using a variable called "filename" doesn't magically create one in the *current* scope.
     
    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
    Ahh apologies,
    I simply thought that by using the parameter filename that it would generate a file of that such name.
    So by using "filename.txt" within the call a new text file should be create?

    Does this file also store within the same local directory as the program itself or is there a standard route folder like C:/programfiles/java that its stored in.

    The reason that I ask is that I have tried this solution and although it compiles with no errors I cannot find the text file. No error message has come up informing me of the programs failure to write.

    regards 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
    A relative path will put the file in a directory relative to the execution context; I don't know how you're executing it, so I don't know where the file is.
     
    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
    I've now attempted to also use a direct reference for the new file
    for example.

    writeResults(arrayList, "C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Nick\\Java\\Test\\filetest.txt");

    The program compiles with no problems, however I cannot find the file anywhere.

    regards 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
    Any exceptions? I assume you looked in the path you listed rather than "anywhere", since there's only one place it would be.
     
    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 of course the referenced route folder is the only place i looked, initially i thought that the file would have been created locally to the java program that references it, however when a file was not created i took the previously mentioned approach. Unfortunately the file is still no where to be seen
     
    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
    Then something else is wrong; check for exceptions, or look again for the file.
     
    Slime does not pay. Always keep your tiny ad dry.
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic