Skip to content

Commit 2624819

Browse files
committed
Added balance problems
1 parent 90fc690 commit 2624819

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

Diff for: Java_Exp_5/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,25 @@ Number of Words = 6
1313
Number of Lines = 3
1414
```
1515

16+
[file_writer_file_reader.java](https://github.com/akkupy/JavaS3/blob/master/Java_Exp_5/file_writer_file_reader.java) - Write a java program that writes your name and address into a file with help of file writer class and read the contents from file and display in the console using appropriate reader class.
17+
```
18+
Enter the name :
19+
Millie
20+
Enter the address :
21+
Mills
22+
Millie
23+
Mills
24+
```
25+
26+
[file_int.java](https://github.com/akkupy/JavaS3/blob/master/Java_Exp_5/file_int.java) - Write a Java program that reads a line of integers from a file, and then displays each integer, and the sum of all the integers (Use String Tokenizer class of java.util).
27+
```
28+
The Numbers in the file are :
29+
1
30+
2
31+
3
32+
4
33+
5
34+
6
35+
7
36+
The sum is 28
37+
```

Diff for: Java_Exp_5/file_int.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class file_int {
4+
public static void main(String[] args) throws Exception {
5+
6+
try {
7+
8+
FileInputStream fin = new FileInputStream("integers.txt");
9+
int ch;
10+
String a;int sum=0;
11+
System.out.println("The Numbers in the file are : ");
12+
while( (ch = fin.read())!=-1 )
13+
{
14+
String s=Character.toString((char)ch);
15+
StringTokenizer st = new StringTokenizer(s);
16+
while( st.hasMoreTokens())
17+
{
18+
a = st.nextToken();
19+
int b = Integer.parseInt(a);
20+
System.out.println(b);
21+
sum = sum + b;
22+
}
23+
}
24+
fin.close();
25+
System.out.println("The sum is " + sum);
26+
}
27+
catch(Exception e) {System.out.print(e);}
28+
}
29+
30+
}
31+

Diff for: Java_Exp_5/file_writer_file_reader.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
import java.io.*;
3+
class file_writer_file_reader {
4+
public static void main(String[] args) throws Exception {
5+
try {
6+
7+
Scanner sc = new Scanner(System.in);
8+
FileWriter fw = new FileWriter("javatext2.txt");
9+
System.out.println("Enter the name :");
10+
String name = sc.nextLine();
11+
System.out.println("Enter the address :");
12+
String address = sc.nextLine();
13+
sc.close();
14+
fw.write(name+"\n");
15+
fw.write(address);
16+
fw.close();
17+
FileReader fr = new FileReader("javatext2.txt");
18+
int ch;
19+
while((ch=fr.read())!=-1) {
20+
21+
System.out.print((char)ch);
22+
23+
}
24+
25+
fr.close();
26+
}
27+
catch(Exception e){System.out.println(e);}
28+
}
29+
}

Diff for: Java_Exp_5/integers.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1 2 3 4
2+
5 6 7

Diff for: Java_Exp_5/javatext2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Millie
2+
Mills

0 commit comments

Comments
 (0)