Java Programming for Android Developers For Dummies, 2nd Edition
Book image
Explore Book Buy On Amazon
"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 android.support.v7.app.AppCompatActivity; import android.os.Bundle;

import android.view.View; import android.widget.EditText;

import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity {

EditText editText;

TextView textView; int myInt = 42; double myDouble = 27649.00; boolean myBoolean = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); textView = (TextView) findViewById(R.id.textView); }

public void onButtonClick(View view) { char myChar = 'B'; String myString = "Hello"; ArrayList myList = new ArrayList(); String[] myArray = {"This ", "is ", "an ", "array."}; textView.append("myInt is " + Integer.toString(myInt) + "\n"); textView.append("myChar is " + Character.toString(myChar) + "\n"); Toast.makeText(this, myString, Toast.LENGTH_LONG).show(); textView.append("myInt + myString + \" \" + myDouble is "); textView.append(myInt + " " + myString + " " + myDouble + "\n"); try { myInt = Integer.parseInt(editText.getText().toString()); } catch (NumberFormatException e) { e.printStackTrace(); } textView.append("myInt is "); if (myInt < 5) { textView.append("small\n"); } else { textView.append("large\n"); }

textView.append("Is myBoolean true? "); if (myBoolean) { textView.append("Yes\n"); }

textView.append("myInt is ");

switch (myInt) { case 1: textView.append("one\n"); break; case 2: case 3: textView.append("a small number\n"); break; default: textView.append("a lot\n"); break; } for (int i = 0; i < 10; i++) { textView.append(Integer.toString(i)); textView.append(" "); }

textView.append("\n"); int i = 0; while (i < 10) { textView.append(i++ + " "); } textView.append("\n"); int j = 0;

do { textView.append(Integer.toString(j++)); textView.append(j <= 9 ? ", " : ""); } while (j < 10); textView.append("\n"); myList.add("Three "); myList.add("cheers "); myList.add("for "); myList.add("Captain "); myList.add("Spaulding"); for (String word : myList) { textView.append(word); }

textView.append("\n"); textView.append(addPeriod("Spaulding")); textView.append("\n"); for (int n = 0; n < myArray.length; n++) { textView.append(myArray[n]); }

textView.append("\n"); }

String addPeriod(String string) { return string + "."; }

}

About This Article

This article is from the book:

About the book author:

Barry Burd, PhD, is a professor in the Department of Mathematics and Computer Science at Drew University in Madison, New Jersey. He has lectured at conferences in the United States, Europe, Australia, and Asia. He hosts podcasts and videos about software and other technology topics. He is the author of many articles and books, including Java For Dummies.

This article can be found in the category: