FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
learnjava/FileInput.java at main · michaelkolesidis/learnjava · GitHub
michaelkolesidis
/
learnjava
Public
Notifications
You must be signed in to change notification settings
Fork
7
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
learnjava
/
FileInput.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (48 loc) · 1.25 KB
Breadcrumbs
learnjava
/
FileInput.java
Copy path
File metadata and controls
52 lines (48 loc) · 1.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* File Input
*
* Take text file filename as argument and calculate the sum of the numbers.
* Each number should be on a seperate line.
*
* Use:
* $java FileInput numbers.txt
*
* numbers.txt:
* 1
* 2
* 3
* 4
* 5
* 6
*
* Output:
* Sum: 21.00
*/
import
java
.
io
.
File
;
// Import the File class
import
java
.
io
.
FileNotFoundException
;
// Import this class to handle errors
import
java
.
util
.
Scanner
;
// Import the Scanner class to read text files
public
class
FileInput
{
public
static
void
main
(
String
[]
args
) {
try
{
double
sum
=
0
;
File
file
;
if
(
0
<
args
.
length
) {
String
filename
=
args
[
0
];
file
=
new
File
(
filename
);
}
else
{
System
.
out
.
println
(
"Please enter filename"
);
return
;
}
Scanner
myReader
=
new
Scanner
(
file
);
while
(
myReader
.
hasNextLine
()) {
double
data
=
Double
.
parseDouble
(
myReader
.
nextLine
());
sum
+=
data
;
}
myReader
.
close
();
System
.
out
.
printf
(
"Sum: %.2f
\n
"
,
sum
);
}
catch
(
FileNotFoundException
e
) {
System
.
out
.
println
(
"An error occurred."
);
e
.
printStackTrace
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL