• 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

Passing a writer from one method to another

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to pass the writer from the encodeFile method to the encodeMessage method so that it prints the encoded message into the new File.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Ian!

I don't agree with that requirement.

Your method is named "encodeMessage". Having that method also output the encoded result to some Writer would be a surprise, because the method's name doesn't suggest any such thing. And then having a method whose purpose is to encode a message AND output it to a writer is not so good, because a method should do only one thing, not two unrelated things.

What I would do would be to change encodeMessage() to return a String which is the encoded version of its input. Then anything which call that method can do anything it likes with the encoded version. It could write the encoded version to a Writer, or insert it into a database, or anything else somebody might think of in the future.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I thought of something similar earlier, but I can't seem to get a String to work in the method since I basically developed it for an input phrase entered by a user from the console. What you're saying, is I should create a String variable in the method, which stores the encoded value. Then, when the encodeFile calls the method, as it does in the while loop, it will "write" the strings value into the File? I just simply don't know how to do this, and I can't find anything about it in the book I'm using. I tried setting the methods paramater to FileWriter writer, but that forced several other problems throughout the code.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I'm confused by that. Especially since when you said "the method" I couldn't tell which method you were talking about.

Here's what I mean: The encodeMessage() method returns a String (the data which it's now System-outing). The encodeFile() method writes that String to its FileWriter.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See, here is where my confusion lies. The encodeMessage method is System-outing an array. How do I take that information and have a string return said value. I wrote the code for the encodeFile() method, the encodeMessage() was provided to me, so maybe I'm not understanding what's in that section completely. Also, if I return a String, don't I have to change the method type from void to String?

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, something like this, which does not work of course.

[code]

private static String encodeMessage(String phrase) {
String line = new String();
line = line.toString();
phrase = phrase.toUpperCase();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isLetter(phrase.charAt(i))) {
for (int j = 0; j < alpha.length; j++) {
if (phrase.charAt(i) == alpha[j]) {
if (j == alpha.length - 1) {
System.out.print(alpha[0]);
} else {
System.out.print(alpha[j + 1]);
}
}
}
} else {
System.out.print(phrase.charAt(i));
}
}
System.out.println();
return line;
}

[code]
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ian Burres wrote:See, here is where my confusion lies. The encodeMessage method is System-outing an array.



It shouldn't be (except possibly for debugging purposes). It should be returning the result of its encoding--whether it's a byte[] or String or whatever.

How do I take that information and have a string return said value.



What do you mean "have a String return said value"? What value? And Strings (or any type for that matter) don't return anything. Methods return values.

I wrote the code for the encodeFile() method, the encodeMessage() was provided to me, so maybe I'm not understanding what's in that section completely.



Then you need to talk to whoever provided encodeMessage(). As Paul said, that method should do just one thing--encode its input--and return the result of that operation. Or, if that method is not going to change its contents, then it should be renamed to something like "printOutEncodedMessage" or somesuch.

Also, if I return a String, don't I have to change the method type from void to String?



Yes, which I believe Paul already suggested you should do.


 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget it. I'm trying to tackle something that is a little beyond where my understanding currently lies. Thanks for your assistance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ian Burres wrote:Forget it. I'm trying to tackle something that is a little beyond where my understanding currently lies. Thanks for your assistance.



Okay, but if you're going to be doing any amount of Java programming, you'll have to learn this. It's quite fundamental. One method does one thing and returns the results. Other methods can use those results to do their one job.

Post again if you're still having problems next time this comes up. Good luck!
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get that about methods. What I don't know how to do is create a darn String and assign the encoded message to it inside the encodeMessage method. I've tried a few things, and this is what I've come up with.

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem is: String line does nothing.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. Then here's an exercise which should be slightly simpler: Write a method which accepts a String and returns a String which contains only the even-numbered characters from the input. For example if the input parameter is "AbcDefG" then the value returned would be "AceG".

The signature of the method should be


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Okay. Then here's an exercise which should be slightly simpler: Write a method which accepts a String and returns a String which contains only the even-numbered characters from the input.



Or just returns the first character.

Or just returns the original String.

Or just returns the String "XYZ".

Start as small as necessary to be able to produce something that works, then keep adding tiny pieces until you get to where you need to go. If you get stuck along the way, post again, but this time you'll be trying to climb a much smaller step, so it will be easier for us to guide you and easier for you to understand what we're saying.

And note that there should be no println() calls in any of these methods, or in your final encode() method (unless you need to put them in there as a form of debugging, so you can see what's happening as the method executes--they don't help the methods do their jobs though).
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic