The original HelloWorld program doesn't do much; it just prints a literal string. That doesn't leave a lot of room for debugging. In this next program, you'll initialize a counter that you'll use to control a loop. Your goal will be to run it without debugging, then later, using the debugger, go in and modify the counter to make the program run differently. But first, you need to copy the program. Once again, I'll provide two options: source entry and import. And once again, I suggest you do both, just for practice. But if you're in a huge hurry, skip to Option 2.
q 8.2(a) Right-click on HelloWorld.java and select Copy.
q 8.2(b) Right-click on your default package and select Paste.
This causes a name conflict, because HelloWorld already exists, so you have to address that in the Name Conflict window:
q 8.2(c) Enter 'HelloWorld2' and click OK.
And that's all it takes to copy a source file!
Caution | Copying a file does not automatically open it! In Figure 8.8, you will see that the new file, HelloWorld2.java, is not open. Only the old file, HelloWorld.java, is open. I have on occasion forgotten this and modified the old program without realizing it! After copying a file, you must open it to edit it. The next steps will open the file. Figure 8.8: After copying the file, the old file is open, but the new file is not. |
Use the popup menu to open the new file (right-click on the file to bring up the popup menu). You can also double-click on a file to use the default editor.
q 8.2(d) Right-click on HelloWorld2.java and select Open.
Now the new file is open.
q 8.2(e) Modify the main method with the following source.
int count = 3; for (int i = 0; i < count; i++) { System.out.println("Hello" + i); } System.exit(0);
This code will use a counter (count) to loop three times, printing a slightly different message each time.
q 8.2(f) Right-click in the editor and select Save.
You can also press Ctrl-S to save the source.
If you've already performed Option 1, you can skip this section and go directly to Step 8.3. You could also execute this step; it won't hurt anything.
q 8.2(g) Right-click on the Hello project and select Import...
q 8.2(h) Select File system and click Next.
q 8.2(i) Use the dropdown for the directory field to select D:\Source\Step 7.
Note | If you haven't already imported Step 7, you may not see anything in your dropdown, so instead just type "D:\Source\Step 7" into the Directory field and press the Tab key. |
q 8.2(j) Left-click on the Step 7 folder.
q 8.2(k) Check the checkbox next to HelloWorld2.java.
q 8.2(l) Click Finish.
If you have already followed the actions in Option 1, you'll get a confirmation dialog.
q 8.2(m) Select Yes to overwrite the existing HelloWorld2.java file.
If you didn't follow Option 1, you'll need to open the file.
q 8.2(n) Right-click on HelloWorld2.java and select Open.
And here is your open file!