• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code compiles fine but stuck in the runtime  RSS feed

 
Pradeep Appala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

The below code is written by me to get a new file that has same contents of an old file but replaces with all specified old strings with new ones. This one doesn't get any compile time errors but doesn't run after providing arguments. Where am I going wrong? Please help!!!

 
paul wheaton
Trailboss
Posts: 22553
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it looks like you told it to exit if the destination file doesn't already exist.

I would think I would want the program to run whether the destination file existed or not.

 
Pradeep Appala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But even if I don't have the destination file, it doesn't create a new one.

I have to manually stop the program from running.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12144
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find it useful to look at what the program is doing while it is running. One tool you should have access to (since it should have been provided with the JDK) is jconsole. Try running it from the command line - you will see a GUI start up, and ask you what process you would like to connect to. Select your "ReplaceText class", and you will be able to view a lot of information about the running process.

Take a look at the "threads" tab, and within that, take a look at the "main" thread. You will probably see something like:



Your line number will probably differ than mine: I removed the package name to make it a little easier on myself. When you run it, you should see that the very last two lines reads something like:

I suspect that this may be where you are experiencing problems - your for loop at line 18 continues until there is nothing more being entered at the command line. But how does it know that there is nothing more to consume? Depending on your operating system you may find that typing Ctrl-D after all 4 parameters may tell the scanner that you are finished entering information.

By the way - I strongly recommend you print an error message before exiting at line 36. Otherwise it may appear that your program ends before it does any work. (See also Paul's comment).

You can read up on the jconsole program in the Oracle jconsole documentation
 
Pradeep Appala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Andrew: Thank you very much for your detailed reply. I found exactly what you said and when I did line by line debugging and found some potential logical flaws too. Corrected some of them. Now it runs according to my requirement. Yes, I have incorporated what Paul mentioned.

Even I use a mac. I will definitely use the processes and threads information from Activity Monitor as suggested by you.


Here is the new code for the program. Also I want to know why the commented code doesn't work instead of the code the while loop. Are there any mistakes in the code? Which methods can I use?


 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12144
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep Appala wrote:Also I want to know why the commented code doesn't work instead of the code the while loop.

What do you mean it doesn't work? Can you provide more details about what you are typing on the command line, and what you think your program is doing?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12144
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I ran this from the command line:

$ java ReplaceText ReplaceText.java ReplaceText.txt Destination output
ReplaceText.txt created.
ReplaceText.txt: Writing the replaced text done.

Which looks like it worked:

$ diff ReplaceText.java ReplaceText.txt
23c23
< System.out.println(" Destination file already exists. ");
---
> System.out.println(" output file already exists. ");
 
Pradeep Appala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Monkhouse wrote:
Pradeep Appala wrote:Also I want to know why the commented code doesn't work instead of the code the while loop.

What do you mean it doesn't work? Can you provide more details about what you are typing on the command line, and what you think your program is doing?


This one doesn't work when compared to the while loop that is there in the previous code quoted.



Previous code that is working:

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12144
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh, sorry, I completely misunderstood.

Take a look at line 7 in your code snippet. Are you sure you only ever want to put the oldString (or the newString in line 4)? Wouldn't you want to output the token you read in at some point?

Also note that A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. As such, the scanner is not going to care whether each word is separated by a space or a tab, or a carriage return or a line feed - they are all the same. You could override the delimiter if you wanted to go that route.
 
Pradeep Appala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I have to take line as a delimiter, then take words in it as tokens and compare them with the string to be replaced. Is this what I have to do?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12144
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like an easy way to solve this assignment.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!