Repository navigation
Expand file tree
/
Copy pathLab1.java
More file actions
75 lines (70 loc) · 2.4 KB
/
Copy pathLab1.java
File metadata and controls
75 lines (70 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Lab1 {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException {
Lab1 obj = new Lab1();
int choice;
while (true) {
System.out.println("**********************");
System.out.println("1. Accept Input from Standard Input Device");
System.out.println("2. Accept Input from File");
System.out.println("3. Exit");
System.out.println("**********************");
System.out.println("Please enter your choice:");
choice = obj.scan.nextInt();
switch (choice) {
case 1:
obj.StdInput();
break;
case 2:
obj.FileInput();
break;
case 3:
System.out.println("You chose exit!");
System.exit(0);
default:
System.out.println("Invalid Option");
}
}
}
void StdInput() {
String org, rev;
int n;
System.out.println("Enter the number of names to be Reversed");
n = scan.nextInt();
scan.nextLine();
while (n != 0) {
System.out.println("Enter the name to be Reversed");
org = scan.nextLine();
rev = Rev_String(org);
System.out.println("Reverse of entered string is: " + rev);
n = n - 1;
}
}
void FileInput() throws IOException {
System.out.println("Enter the input file name(with extension)");
String infile = scan.next();
System.out.println("Enter the output file name(with extension)");
String outfile = scan.next();
String org, rev;
BufferedReader br = new BufferedReader(new FileReader(infile));
PrintWriter pw = new PrintWriter(outfile);
while ((org = br.readLine()) != null) {
rev = Rev_String(org);
pw.println(rev);
}
pw.flush();
System.out.println("All Reverse Names written to " + outfile);
br.close();
pw.close();
}
String Rev_String(String org) {
StringBuilder sb = new StringBuilder(org);
sb.reverse();
return sb.toString();
}
}