Java Textbook
- Rephactor - electronic Java
textbook that "refactors" how textbooks are done
Peer Tutoring
- Villanova CSC Peer
Tutors - for extra help on lab assignments, find Peer Tutors in the
list who offer help in CSC 2014, 1051 or 1052, all of which cover Java
Java IDEs
- Eclipse - the most widely used by
professional developers
- JGrasp - used in many CS courses at
Villanova and elsewhere
Java
Solving Common Problems
- I'm trying to read an integer and a string from System.in, but as
soon as I read in the number the program doesn't let me read in the
string. How can I fix it?
// In this version, the scanner doesn't wait to read
the string!
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:"); int number = in.nextInt();
System.out.println("Enter a name:"); String name = in.nextLine();
System.out.println("name:"+name+" number:"+number);
|
The problem is that the call to nextInt doesn't consume the newline
character that you typed, so nextLine gobbles it up right away. To fix
it, add an extra call to nextLine first to consume the newline character
like this:
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:"); int number = in.nextInt();
in.nextLine(); // gobble up the leftover
newline
System.out.println("Enter a name:"); String name = in.nextLine();
System.out.println("name:"+name+" number:"+number);
|
- How can I open a file, read each line in turn, and do something with
each line?
The issue with reading in line after line can usually be addressed by
always making sure the Scanner has another line available to read before
trying to read it. This example shows how to open a file and read in
each line from the file, and in this case print out the line.
File input = new File("data.txt"); Scanner scanner = new Scanner(input); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } |
- How can I open a file and read a number of
differnt kinds of values on
each line in the file, repeating this for many lines?
In this
case, it isn't enough to see if the scanner has another line available
to read. We need to see if it has a token to read, so instead of hasNextLine use
hasNext, like this:
File input = new File("data.txt"); Scanner scanner = new Scanner(input); while (scanner.hasNext()) { int val1 = scanner.nextInt();
boolean val2 = scanner.nextBoolean();
String val3 = scanner.next();
boolean val4 = scanner.nextBoolean();
System.out.println("line:"+val1+","+val2+","+val3+","+val4); } |
- What should I do if my program can't seem to find
the data file that I want to read in?
This problem is almost always the result of the data file being
somewhere other than your program thinks it is. For example, in Eclipse,
the file should be at the Project level rather than at the src
level.
|
|
WRONG PLACE! |
RIGHT PLACE! |
- How can get started writing a GUI application?
The recommended approach is to start with a "hello world" version of a
GUI application or other simple, working one, and modify it to your
liking. One example that fills this bill is the
PushButton.java demo.
- How can I count occurrences of words and sort them
by count?
An excellent option in Java is the TreeMap class which makes it
easy to keep a count of things and to sort by value rather than key. An
example of this is the
SortedMapExample.java demo.
|