Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab(03)

Example :1 DataSetTesterApp.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

class DataSetReader {
private double[] data;
public double[] readFile(String filename) throws IOException {
FileReader reader = new FileReader(filename);
try {
Scanner in = new Scanner(reader);
readData(in);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
reader.close();
}
return data;
}

private void readData(Scanner in) throws Exception {


if (!in.hasNextInt()) {
throw new Exception("Length expected");
}
int numberOfValues = in.nextInt();
data = new double[numberOfValues];
for (int i = 0; i < numberOfValues; i++) {
readValue(in, i);
}
if (in.hasNext()) {
throw new Exception("End of file expected");
}
}

private void readValue(Scanner in, int i) throws Exception {


if (!in.hasNextDouble()) {
throw new Exception("Data value expected");
}
data[i] = in.nextDouble();
}

}
public class DataSetTesterApp {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
DataSetReader reader = new DataSetReader();
boolean done = false;
while (!done) {
try {
System.out.println("Please enter the file name: ");
String filename = in.next(); // = "C:\\abc.txt";
double[] data = reader.readFile(filename);
double sum = 0;
for (double d : data) //loop through elements in array
{
sum = sum + d;
}
System.out.println("The sum is " + sum);
done = true;
} catch (FileNotFoundException exception) {
System.out.println("File not found.");
} catch (IOException exception) {
exception.printStackTrace();
} catch (Exception exception) {
System.out.println("Bad data: " + exception.getMessage());
}
}
}
}
Example :2 ParseInts.java

import java.util.Scanner;
public class ParseInts {
public static void main(String[] args) {
int val = 0, sum = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a line of text");
String line = scan.nextLine();
Scanner scanLine = new Scanner(line);
while (scanLine.hasNext()) {
try {
val = Integer.parseInt(scanLine.next());
} catch (NumberFormatException x) {
val = 0;
}
sum += val;
}
System.out.println("The sum of the integers on this line is " + sum);
}
}
Example :3

Factorials.java

import java.util.Scanner;

class MathUtils {

public static int factorial(int n) throws RuntimeException {

if (n > 0) {

if (n < 16) {

int fac = 1;

for (int i = n; i > 0; i--) {

fac *= i;

return fac;

} else {

throw new IllegalArgumentException("Number Should be less than 16");

} else {

throw new IllegalArgumentException("Number Should be Positive ");

public class Factorials {

public static void main(String[] args) {

String keepGoing = "y";

Scanner scan = new Scanner(System.in);

while (keepGoing.equals("y") || keepGoing.equals("Y-5")) {

System.out.print("Enter an integer: ");

int val = scan.nextInt();

try {
System.out.println("Factorial(" + val + ") = "

+ MathUtils.factorial(val));

} catch (IllegalArgumentException ex) {

System.out.println(ex.getMessage());

System.out.print("Another factorial? (y/n) ");

keepGoing = scan.next();

You might also like