import java.io.*;
import java.util.*;
public class WordsInFile{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
Scanner fileIn;
String word;
int numWords = 0;
try{
System.out.print("Enter a filename: ");
fileIn = new Scanner(new FileReader(stdIn.nextLine()));
while(fileIn.hasNext()){
word = fileIn.next();
numWords++;
}
System.out.print("Number of words = " + numWords + "\n");
fileIn.close();
}
catch(FileNotFoundException e){
System.out.println("Invalid filename.");
}
catch(Exception e){
System.out.println("Error reading from the file.");
}
}//end of main
}//end of WordsInFile class
Friday, November 27, 2009
Chapter 15 Exercise 4
import java.util.Scanner;
import java.io.*;
public class TextWriter{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
/*String[] churchill =
{"Before Alamein we never had a victory.",
"After Alamein we never had a defeat."};*/
int year = 1942;
PrintWriter fileOut;
try{
System.out.print("Enter filename: ");
//fileOut = new PrintWriter(stdIn.nextLine());
fileOut = new PrintWriter(new FileWriter(stdIn.nextLine(), true));
fileOut.print(year);
fileOut.close();
//for(String line : churchill){
//fileOut.println(line);
//}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
}
}//end of main
}//end of TextWriter class
import java.io.*;
public class TextWriter{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
/*String[] churchill =
{"Before Alamein we never had a victory.",
"After Alamein we never had a defeat."};*/
int year = 1942;
PrintWriter fileOut;
try{
System.out.print("Enter filename: ");
//fileOut = new PrintWriter(stdIn.nextLine());
fileOut = new PrintWriter(new FileWriter(stdIn.nextLine(), true));
fileOut.print(year);
fileOut.close();
//for(String line : churchill){
//fileOut.println(line);
//}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
}
}//end of main
}//end of TextWriter class
Chapter 15 Exercise 3
import java.util.Scanner;
import java.io.*;
public class TextWriter{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
String[] churchill =
{"Before Alamein we never had a victory.",
"After Alamein we never had a defeat."};
PrintWriter fileOut;
try{
System.out.print("Enter filename: ");
fileOut = new PrintWriter(stdIn.nextLine());
for(String line : churchill){
fileOut.println(line);
}
fileOut.close();
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
}//end of main
}//end of TextWriter class
import java.io.*;
public class TextWriter{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
String[] churchill =
{"Before Alamein we never had a victory.",
"After Alamein we never had a defeat."};
PrintWriter fileOut;
try{
System.out.print("Enter filename: ");
fileOut = new PrintWriter(stdIn.nextLine());
for(String line : churchill){
fileOut.println(line);
}
fileOut.close();
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
}//end of main
}//end of TextWriter class
Chapter 14 Exercise 3
import java.util.Scanner;
public class Division{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
int n, d, q;
try{
System.out.print("Enter numerator: ");
n = stdIn.nextInt();
System.out.print("Enter divisor: ");
d = stdIn.nextInt();
q = n / d;
System.out.println(q + "\n");
}
catch(ArithmeticException e){
System.out.println("Error, but keep going anyway,");
}
}//end of main
}//end of Division class
public class Division{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
int n, d, q;
try{
System.out.print("Enter numerator: ");
n = stdIn.nextInt();
System.out.print("Enter divisor: ");
d = stdIn.nextInt();
q = n / d;
System.out.println(q + "\n");
}
catch(ArithmeticException e){
System.out.println("Error, but keep going anyway,");
}
}//end of main
}//end of Division class
Subscribe to:
Posts (Atom)