Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

"I have to create an enhanced for statement. Can someone remind me how to code an enhanced for statement? And while you're at it, how can I catch an exception? Where can I find all that Java stuff quickly?"

You find all that stuff right here in this cheat sheet. The Java program in this cheat sheet contains snippets of code representing many of Java's most important language features, including switches, strings, breaks, if . . . else statements, and arrays. As an added bonus, the program even contains a sly reference to that classic Marx Brothers movie, Animal Crackers. Enjoy!

package com.example.cheetsheet;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class CheatSheet {
  int myInt = 42;
  double myDouble = 27649.00;
  boolean myBoolean = true;
  public static void main(String[] args) {
    new CheatSheet();
  }
  public CheatSheet() {
    char myChar = 'B';
    String myString = "Hello";
    ArrayList<String> myList = new ArrayList<String>();
    String[] myArray = 
        { "This ", "is ", "an ", "array." };
    System.out.println(myInt);
    System.out.println(myChar);
    JOptionPane.showMessageDialog(null, myString);
    System.out.println
        (myInt + " " + myString + " " + myDouble);
    String stringIn = JOptionPane
        .showInputDialog("Enter a number from 1 to 10");
    try {
      myInt = Integer.parseInt(stringIn);
    } catch (NumberFormatException e) {
      e.printStackTrace();
    }
    if (myInt < 5) {
      System.out.println("Small");
    } else {
      System.out.println("Large");
    }
    if (myBoolean) {
      System.out.println("Yes");
    }
    switch (myInt) {
    case 1:
      System.out.println("One");
      break;
    case 2:
    case 3:
      System.out.println("A small number");
      break;
    default:
      System.out.println("A lot");
      break;
    }
    for (int i = 0; i < 10; i++) {
      System.out.print(i);
      System.out.print(" ");
    }
    System.out.println();
    
    int i = 0;
    while (i < 10) {
      System.out.print(i++ + " ");
    }
    System.out.println();
    
    int j = 0;
    do {
      System.out.print(j++);
      System.out.print(j <= 9?", ":"");
    } while (j < 10);
    System.out.println();
    
    myList.add("Three ");
    myList.add("cheers ");
    myList.add("for ");
    myList.add("Captain ");
    myList.add("Spaulding");
    for (String word : myList) {
      System.out.print(word);
    }
    System.out.println();
    System.out.println(addPeriod("Spaulding"));
    for (int n = 0; n < myArray.length; n++) {
      System.out.print(myArray[n]);
    }
    System.out.println();
  }
  String addPeriod(String string) {
    return string + ".";
  }
}
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

SERIES
Java Programming for Android Developers For Dummies Cheat Sheet

Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.