Brandon Rozek

Thoughts on Web Development, Statistics, and Linux

Posts

Obtaining Command Line Input in Java

To obtain console input for your program you can use the Scanner class

First import the relevant library


import java.util.Scanner;

Then create a variable to hold the Scanner object


Scanner input;
input = new Scanner(System.in);

Inside the parenthesis, the Scanner binds to the System input which is by default the console

The new varible input now has the ability to obtain input from the console. To do so, use any of the following methods

Method What it Returns
next() The next space seperated string from the console
nextInt() An integer if it exists from the console
nextDouble()              A double if it exists from the console
nextFloat() A float if it exists from the console
nextLine() A string up to the next newline character from the console
hasNext() Returns true if there is another token
close() Unbinds the Scanner from the console

Here is an example program where we get the user’s first name


import java.util.Scanner;

public class GetName {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter your name: ");
    String firstName = input.next();
    System.out.println("Your first name is " + firstName); 
  }
}

Escape Sequences in Java

Sometimes you want to format your outputs. This is a quick cheatsheet containing the differerent escape sequences

 

Character Escape Sequence
Newline \n
Tab \t
Backspace \b
Double Quote            \”
Single Quote \’
Backslash \\

Java Swing Components

This post, over time, will serve as a reference to myself and others of the different UI components available in the Swing library. This post assumes a general familiarity with setting up a basic Swing application and focuses only on the individual components.

Read More →

Using System Themes In Java Swing

The default theme for Java Swing components is a cross-platform theme called “Metal”. I use the Adapta theme for GTK on Linux and this theme does not match at all what my other GUI applications look like. So here, I will describe a simple way to utlize already existent system themes in Java Swing applications.

Read More →

Viewing Java Applets

When you use an IDE there are many things you can take for granted. A section of an intro level computer science course at my university uses JGrasp to build Java Applets.

Following around using a normal text editor, I found that I couldn’t just compile and run the code like I have with my java programs in the past. To be able to help around and assist in the course, I need to be able to build and run these applications. The rest of this article describes the process I underwent to be able to use my existing setup to write and build java applets. Of course you can always install JGrasp and have that all built in, but it’s always nice to not have to change your workflow.

Read More →