user2723834
user2723834

Reputation: 3

reading files and averging, keeps asking for user input

I am supposed to ask the user for the name of the file, then read the file and average the numbers in the file, my program just keeps asking for the name of the file.

import java.util.Scanner;
import java.io.*;

public class A13_Average
{
public static void main(String[] args)
{
    getFile();
}

public static void getFile()
{   
    String name;
    int sum = 0;
    int num = 0;
    try
    {
                    //Gets the user to input name of file
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the name of your file: ");
        name = in.nextLine();
        //gets each line of numbers
        Scanner input = new Scanner(new File(name));
        input.nextInt();

        while(input.hasNextLine());
        {
                             //sum of the numbers
             sum += input.nextInt();
                             //number of numbers
             num += 1;
        }

        System.out.println("The average of your numbers is: ");
        System.out.printf("#0.00", (sum/num));
        in.close();
    }
    catch (Exception i)
    {
        System.out.println("Error: " + i.getMessage());
    }
}
}

This is the numbers.txt the file to test with:

10
84
39
93
34
14
11
33
88
83
28
97
75
49
32
69
80
95
48
19
33
85
48
20
48
22
87
72
9
55
14
21
79
94
40
99
90
1
1
66
38
76
59
57
29
29
32
71
 2
72
13
74
35
24
29
24
21
78
76
52
74
29
25
54
100
96
59
55
9
71
1
99
88
82
24
38
72
10
3
8
43
43
49
28
6
48
15
27
93
34
4
87
73
24
28
12
52
66
10
63
8
34
100
2
56
22
89
96
56
27
83
29
22
98
80
96
64
17
84
39
82
16
62
99
88
85
52
72
15
25
48
14
10
82
21
75
50
40
99
92
27
86
66
38
79
85
54
94
46
93
34
15
31
57
31
55
10
99
90
100
99
95
47
5
98
85
48
17
90
2
27
92
17
87
74
34
8
36
42
30
46
89
92
26
64
15
46
95
52
72
14
16
68
63
11
39
88
77
71
100
96
62
93
32
82
21
60
71
6
53
90
2
28
3
21
78
78
78
77
64
19
25
52
69
74
33
85
48
15
45
80
96
58
53
82
19
33
97
68
64
25
49
25
57
41
14
3
9
75
42
31
64
19
19
34
7
9
56
22
88
81
6
48
22
87
74
32
83
35
22
96
61
90
100
97
75
41
10
92
21
70
91
13
89
94
38
77
63
12
59
57
30
49
32
72
9
73
20
56
17
91
11
38
71
6
75
46
90
98
82
19
23
1
63
3
13
83
29
23
9
67
60
72
17
74
34
11
33
87
73
28
11
26
66
46
90
99
93
32
81
13
72
16
63
5
4
43
54
91
8
34
1
4
72
8
24
28
97
73
20
59
62
100
1
49
29
22
84
44
66
47
5
1
93
28
98
76
58
45
70
85
48
13
75
41
12
49
34
8
20
50
48
10
5
15
41
10
3
27
95
48
18
11
36
45
78
74
36
40
9
53
84
45
80
97
72
9
57
36
38
64
22
93
28
7
94
37
58
46
91
10
87
68
63
5
37
49
26
61
88
81
11
19
27
91
12
54
1
3
12
52
65
28
98
80
98
77
69
73
27
82
23
12
66
45
70
93
31
51
59
55
10
1
75
49
35
18
97
70
86
59
65
35
31
54
100
98
76
58
43
41
21
78
82
16
48
12
50
46
84
45
75
43
48
22
97
70
91
10
7
94
39
94
44
65
33
100
98
84
36
43
50
42
25
44
60
77
65
30
43
46
86
62
100
1
49
29
22
84
44
66
47
5
1
93
28
98
76
58
45
70
85
48
13
75
41
12
49
34
8
20
50
48
10
5
15
41
10
3
27
95
48
18
11
36
45
78
74
36
40
9
53
84
45
80
97
72
9
57
36
38
64
22
93
28
7
94
37
58
46
91
10
87
68
63
5
37
49
26
61
88
81
11
19
27
91
12
54
1
3
12
52
65
28
98
80
98
77
69
73
27
82
23
12
66
45
70
93
31
51
59
55
10
1
75
49
35
18
97
70
86
59
65
35
31
54
100
98
76
58
43
41
21
78
82
16
48
12
50
46
84
45
75
43
48
22
10
70
91
10
7
94
39
94
44
65
33
100
98
84
36
43
50
42
25
44
60
77
65
30
43
46
86
62
100
1
49
29
22
84
44
66
47
5
1
93
28
98
76
58
45
70
85
48
13
75
41
12
49
34
8
20
50
48
10
5
15
41
10
3
27
95
48
18
11
36
45
78
74
36
40
9
53
84
45
80
97
72
9
57
36
38
64
22
93
28
7
94
37
58
46
91
10
87
68
63
5
37
49
26
61
88
81
11
19
27
91
12
54
1
3
12
52
65
28
98
80
98
77
69
73
27
82
23
12
66
45
70
93
31
51
59
55
10
1
75
49
35
18
97
70
86
59
65
35
31
54
100
98
76
58
43
41
21
78
82
16
48
12
50
46
84
45
75
43
48
22
97
70
91
10
7
94
39
94
44
65
33
100
98
84
36
43
50
42
25
44
60
77
65
30
43
46
86
62
100
1
49
29
22
84
44
66
47
5
1
93
28
98
76
58
45
70
85
48
13
75
41
12
49
34
8
20
50
48
10
5
15
41
10
3
27
95
48
18
11
36
45
78
74
36
40
9
53
84
45
80
97
72
9
57
36
38
64
22
93
28
7
94
37
58
46
91
10
87
68
63
5
37
49
26
61
88
81
11
19
27
91
12
54
1
3
12
52
65
28
98
80
98
77
69
73
27
82
23
12
66
45
70
93
31
51
59
55
10
1
75
49
35
18
97
70
86
59
65
35
31
54
100

Upvotes: 0

Views: 116

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208964

Here's your problem

  while(input.hasNextLine());

Delete the ;

  while(input.hasNextLine())

The ; is an ending statement

Upvotes: 1

Related Questions