Tuesday, August 26, 2014

NetBeans 8 Basic Console Input Output [CIO]

---
NetBeans 8 Basic Console Input Output [CIO]

STEPS

1) Start NetBeans.

2) Create a new project.

2-1) Select Java Application.
2-2) Type the name MyConsole1.
2-3) Project is created.
        

3) Edit Codes

3-1) Replace the line “// TODO …” with the following codes.
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter your weight: ");
    int weight = sc.nextInt();
    System.out.println("Weight = " + weight);
Press SHIFT-ALT-F to realign your codes.
3-2) Notice some syntax errors.
3-3) If you hover your mouse over the red underlined text, you will get some help message.
3-4) Choose “Add import for java.util.Scanner”
3-5) NetBeans add the import line.
3-6) Run.
3-6-1) You will see the prompt “Please enter your weight:”
3-6-2) Click an area just after the colon, type 60 and press ENTER key.
3-7) Run again but this time enter “sixty” instead of a number 60.
3-8) You will get an error message.
3-9) Replace the codes in Step 3-1.
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter your weight: ");
    int weight;
    // Accept input and test to see if it’s an integer
    if (sc.hasNextInt()) {
       // Success, it was an integer so get it
       weight = sc.nextInt();
       System.out.println("Weight = " + weight);
    } else {
        // Failure so retrieve the input as a String
        String bad = sc.next();
        // Inform the user what went wrong
        System.out.println(bad + " cannot be converted to an integer");
    }
3-10) Run.
3-11) Enter “sixty”.
3-12) Finally, replace the current codes with the following
    Scanner sc = new Scanner(System.in);
    System.out.println("Select an Account");
    System.out.println("A: Savings");
    System.out.println("B: Checking");
    System.out.println("C: Exit");
    System.out.print("Please choose A, B or C: ");
    char letter;
    if (sc.hasNext("[A-Ca-c]")) {
        String choice = sc.next();
        letter = choice.toUpperCase().charAt(0);
        System.out.println("You chose: " + letter);
    } else {
        String choice = sc.next();
        System.out.println(choice + " is not valid input");
    }
3-13) Outcome:

---
Source: http://netbeans.dzone.com/nb-class-console-input-regular-exp 
---

NetBeans 8 Build Run Distribute Java Application

---
NetBeans 8: Build, Run and Distribute Java Application
Once you are satisfied that your application works properly, you can prepare the application for deployment outside of the IDE. In this section you will build the application's JAR file and then run the JAR file from the command line.

STEPS

1) START NETBEANS

Continue from the previous tutorial or download startup file.

2) Building the Application

The main build command in the IDE is the Clean and Build command. The Clean and Build command deletes previously compiled classes and other build artifacts and then rebuilds the entire project from scratch.
Notes: There is also a Build command, which does not delete old build artifacts, but this command is disabled by default. See About Building Java Projects in Developing Applications with NetBeans IDE for more information.
To build the application:
2-1) Choose Run > Clean and Build Project (Shift-F11).
2-2) Output from the Ant build script appears in the Output window.
If the Output window does not appear, you can open it manually by choosing Window > Output > Output.
When you clean and build your project, the following things occur:
  1. Output folders that have been generated by previous build actions are deleted ("cleaned"). (In most cases, these are the build and dist folders.)
  2. build and dist folders are added to your project folder (hereafter referred to as the PROJECT_HOME folder). You can view these folders in the Files window.

  3. All of the sources are compiled into .class files, which are placed into the PROJECT_HOME/build folder.

  4. A JAR file containing your project is created inside the PROJECT_HOME/dist folder.
  5. If you have specified any libraries for the project (in addition to the JDK), a lib folder is created in the dist folder. The libraries are copied into dist/lib.

  6. The manifest file in the JAR is updated to include entries that designate the main class and any libraries that are on the project's classpath.
Note: You can view the contents of the manifest in the IDE's Files window. After you have built your project, switch to the Files window and navigate to dist/MyApp.jar.
2-3) Expand the node for the JAR file (MyApp.jar), expand the META-INF folder, and double-click MANIFEST.MF to display the manifest in the Source Editor.
Notice the following items:
Main-Class: acrostic.Main
Class-Path: lib/MyLib.jar

3) Running the Application outside of the IDE

To run the application outside of the IDE:
3-1) On your system, open up a command prompt or terminal window.
3-2) In the command prompt, change directories to the MyApp/dist directory.
3-3) At the command line, type the following statement:
             java -jar MyApp.jar However we all feel zealous    
3-4) The application then executes and returns the following output as shown in the image below:
Result = Hello
           
3-5) Command prompt showing the output from the running program.

4) Distributing the Application to Other Users

Now that you have verified that the application works outside of the IDE, you are ready to distribute the application.
To distribute the application:
4-1) On your system, create a zip file that contains the application JAR file (MyApp.jar) and the accompanying lib folder that contains MyLib.jar.
4-2) Send the file to the people who will use the application. Instruct them to unpack the zip file, making sure that the MyApp.jar file and the lib folder are in the same folder.
4-3) Instruct the users to follow the steps in the Running the Application Outside of the IDE section above.

DOWNLOAD: Completed Project File

---
Source: https://netbeans.org/kb/docs/java/javase-intro.html 
---

Monday, August 25, 2014

NetBeans 8 Test and Debug Java Applications

---
NetBeans 8 Test and Debug Java Applications

STEPS

1) Start NetBeans

Continue from the previous tutorial or download startup file.

2) Testing the Application

Now you will create and run a test for the project using JUnit and then run the application in the IDE's debugger to check for errors. In the JUnit test, you will test the LibClass by passing a phrase to the acrostic method and using an assertion to indicate what you think the result should be.

2-1) Creating JUnit Tests

2-1-1) Right-click the LibClass.java node in the Projects window and choose Tools >Create/Update Tests (Ctrl-Shift-U).
2-1-2) In the Create Tests dialog box, click OK to run the command with the default options.
Note: If this is the first time you have created JUnit tests in the IDE, you will be prompted with the Select JUnit Version dialog box. Press Enter to select JUnit 4.x and continue to the Create Tests dialog box.
2-1-3) The IDE creates the org.me.mylib package and the LibClassTest.java file in a separate test folder. You can find this file by expanding the Test Packages node and the org.me.mylib subnode.
2-1-4) In LibClassTest.java, delete the body of the public void testAcrostic() method.
2-1-5) In place of the deleted lines, type or paste in the following:
System.err.println("Running testAcrostic...");
String result = LibClass.acrostic(new String[]
                  {"fnord", "polly", "tropism"});
                assertEquals("Correct value", "foo", result);
2-1-6) Save the file by pressing Ctrl-S.

2-2) Running JUnit Tests

2-2-1) Select the MyLib project node and choose Run > Test Project (MyLib) or press Alt-F6.
2-2-2) The MyLib (test) tab opens in the Output window. The JUnit test cases are compiled and run. The JUnit test result shows that the test passes.
2-2-3) You can also run a single test file rather than testing the entire project. Select the LibClass.java tab in the Source Editor and choose Run > Test File.
The JUnit API documentation is available from the IDE. Choose Help > Javadoc References > JUnit API.
Note: If this is the first time you access Javadoc in the IDE, you need to first choose Help > Javadoc References > More Javadoc. Click Cancel in the Javadoc References dialog box. Then choose Help > Javadoc References > JUnit API.
You can learn more about JUnit by visiting http://www.junit.org

3) Debugging the Application

In this section, you will use the debugger to step through the application and watch the values of variables change as the acrostic is assembled.
To run the application in the debugger:
3-1) In the LibClass.java file, go to the acrostic method and place the insertion point anywhere inside b.append(args[i].charAt(i));.
3-2) Then press Ctrl-F8 to set a breakpoint.
3-2) Select the MyApp project node and choose Debug > Debug Project (Ctrl-F5).
3-3) The IDE opens the Debugger windows and runs the project in the debugger until the breakpoint is reached.
3-3) Select the Local Variables window (available under submenu Debugging).
3-4) Expand the args node.
3-5) The array of strings contains the phrase you entered as the command arguments.
3-4) Press F7 (or choose Debug > Step Into) to step through the program.
3-5) Watch the b variable change as the acrostic is constructed.
3-5) When the program reaches the end, the debugger windows close.
For more information, see Writing JUnit Tests in NetBeans IDE.

DOWNLOAD: Completed Project File

---
Source: https://netbeans.org/kb/docs/java/javase-intro.html 
---