Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me fix this code, please.  RSS feed

 
Varun Goenka
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, this code is unreadable. Why is all indentation removed?

Second of all, what is it supposed to do, and what does it not do? http://faq.javaranch.com/java/TellTheDetails
 
Varun Goenka
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The idea is to get used to file input and output streams.If that makes you angry, sorry, but I could still use some help.

Getting down to buisness, what this code does is, accept a file read it print it and then edit it ,IF THE USER WILLS SO.

The complication probably arises because I wanted to make sure the program does not terminate incase the user plans to input an alphabet incase of an int.

Which is why you'll probably see many do ......whiles.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12088
246
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see anywhere that Rob was getting angry - he was simply trying to tell you what is needed before anyone can even start to help you.

I have reformatted your code so that I can read it. Unfortunately that is about all I have time for today - if I had not needed to make the code readable then I might have had time to look at what the problems are.

Some quick thoughts:
  • This code will not compile. You have told us what you would like the program to do, but you have not told us what your problem is. I can therefore only assume that the problem is that you cannot get it to compile.


  • Regarding compilation:
  • You have variables varfw and varfr that you are using in lines 48, 88 and 108; however they are not in scope at the time you are using them. You have defined them on lines 32 & 33, however they are defined within the try...catch block. That means that as soon as you are past line 37 the variables varfw and varfr no longer exist.
  • Creating a FileWriter (line 33) can potentially throw IOException - you are not catching this or declaring it to be thrown


  • General issues:
  • Variable varfile (line 27) is not used - did you have a purpose for this?
  • Variable gostrng4 (line 63 & line 98) is never read - did you have a purpose for this?
  • There is no point in initializing vstring with a String on line 15 since it is overwritten in line 22. You might as well initialize it to null
  • Same applies to str3 on line 63. str3 is only used when count2 is equal to 1, so you could change the scope to just that block.
  • readLine on line 67 could return a null - you are not handling this
  • Your do...while loop from lines 76 - 99 will only terminate when gostrng is set to true. However you never set gostrng in that loop - it will run forever.
  • Your do...while loop from lines 79 - 86 will only terminate when gostrng3 is set to false. However you never set gostrng3 in that loop - it will run forever.
  •  
    Rob Spoor
    Sheriff
    Posts: 20893
    81
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Varun Goenka wrote:If that makes you angry, sorry,

    No need to apologize. As Andrew said, I was not angry. I was just a) stating a lack of indentation which only makes it harder to read, and b) asking for more information.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!