Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using if statements with BufferedWriter/BufferedReader  RSS feed

 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am attempting to write code for a Java project which is to have two text files in which will read from an existing one. If data is greater than - it goes into one file, if it is less than - it goes into the other. I know this is like pseudo code here, but I have tried several things already, but isn't working yet. So far, I do have one file printing, but it includes everything from my file that is read from and nothing on the other file. My question is: Although I am an extremist when it comes to researching, I have not been able to find enough information on how to use an if statement while using


I would greatly appreciate any advice or links that would fully explain and/or show examples for how this can work. My text vaguely touches on this subject! By the way, I am a beginner/fourth class in (going into intermediate/depending on how one is categorized) .

Thank you much!

BTW, if this is not the correct place for it; kindly post me the link in which it should be placed. Thanks

 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
Can you show us the code you have? We love to help, but since this is homework, we ask that you do the work.
 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe;
Thanks! And I totally agree and understand that I need to do my own work , thats why I asked for links and such and did not post my code at first; but only what I thought was needed for people to understand what I was talking about. Here is what I have so far:



JCE: Code redacted on request from instructor
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The first problem I see is, this isn't an if-statement at all, it's a comment. It won't do anything at all.

But I expect you knew that. Problem is, you haven't shown us the code which has a problem, so that makes it difficult to provide suggestions about what to do about said code. We're left to guess. So let me guess: your if-statement looked like this:



This is a fairly common blunder for beginners. If, and only if, the boolean expression in an if-statement is true, then the statement following the if-statement is executed. In this case that statement is this:



This is an example of the empty statement, which as you might guess doesn't do anything. So you're saying if Population > 50000 then don't do anything, otherwise don't do anything. Not what you wanted.

An if-statement (complete with else-clause) should look like this:



You should always put those {braces} in there, even if there's only one statement inside them. If you say "oh, there's only one statement, I don't need the braces in that case" then a week or a month from now you're going to add a second statement and forget to add the braces that you do need now.
 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul, I have decent knowledge about the if statements; however, the first one you mentioned was commented out because it wasn't working. Since reviewing your reply, I have implemented the if statements (2 of them because an else gave me red lines/errors). I now have my program printing to both files, however, it is only one of the cities (the top one listed on my CityInfo) and get this error: Error: Stream closed and not sure if after I get this fixed, if the program will print the <> to the correct files. Are there any links you can provide for me to look further into how I can use the If statement with BufferedWriter/Reader? Below is my code; minus the imports.

Thanks again I feel like I am almost there!



JCE: Code redacted on request from instructor
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kimberly Mack wrote:Are there any links you can provide for me to look further into how I can use the If statement with BufferedWriter/Reader?


You are asking for a really, really specific use of the language. If such an example existed (and I don't think it does), it would be practically indistinguishable from your code (or the correct solution). If we linked to it, it would be like giving you the answer. You'd be no closer to understanding what you're coding.
Did you post your latest code? I thought Paul already pointed out the problem with this line:


 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had modified my code after reviewing Paul's suggestion and posted what I had from that point....since then, I have made further changes and more progress. Maybe I'm asking too much for my programming level...and should be thinking in more simpler terms (but how are we to learn if not given the resources to do so?) ...however, this is what my last syntax looks like (shown below). I have info printing in the console now and have data printing to both files as needed, however, the data is not being considered as to what their population is. My error is: Error:Stream closed




JCE: Code redacted on request from instructor
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kimberly Mack wrote:Are there any links you can provide for me to look further into how I can use the If statement with BufferedWriter/Reader?


No. That's like saying to your driving instructor "OK, now that I know how to drive, can you teach me how to drive to school?"

Generally in the computing business, people design things to work independently of each other. So an if-statement is an if-statement, and it doesn't matter whether it controls code which uses a BufferedReader or code which uses anything else. Likewise a BufferedReader is designed to do some particular thing, and it doesn't matter whether you use an if-statement to make it do that thing or not.

So when you find yourself asking how to use A with B, and there's no in-your-face obvious relationship between A and B, you're asking a wrong question. Instead you should be separating the two things in your mind.
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to come up with a car metaphor

When I get stuck, I'll write a quick little program to make sure I'm not crazy. Try saving this code in a file, compiling it and running it:

What did you expect to happen? What happened? Why?
 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:



Thank you Paul, I did not see what you meant by this at first, and it really is such a big problem; for beginner/intermediate programmers and maybe for experienced ones as well. With a fresh set of eyes and just taking a breather, these mistakes can be found. Thanks again.
 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ess wrote:I was trying to come up with a car metaphor

When I get stuck, I'll write a quick little program to make sure I'm not crazy. Try saving this code in a file, compiling it and running it:

What did you expect to happen? What happened? Why?


Thank you Joe, I appreciate you bringing "keeping it simple" to mind. I was attempting to do everything in one class whereas my final product consisted of three classes--one being a test class and the other containing setters and getters. The if statement; as was mentioned in this topic, was able to fit into my project as well. Another issue I had, in the end after cleaning up my code, I noticed a brace was out of place also. This disabled the entire program from running! How awful to have everything right and a grammatical error keep you from having a successful run
There really should be more information that one can read on Java.
Thanks again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!