forked from samuelstacey/Financial-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTips.java
More file actions
35 lines (33 loc) · 977 Bytes
/
Tips.java
File metadata and controls
35 lines (33 loc) · 977 Bytes
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
/**
* Tips
* @author Luke
*/
public class Tips {
Scanner scanner;
/**
* Get a random tip or motivational quote
*
* @param fileName name of file as it appears in viewer (without .txt)
* @return Contains the quote/tip
* @throws FileNotFoundException .
*/
public String getTipMotivation(String fileName) throws FileNotFoundException {
String tip = null;
Random rand = new Random();
int n = 0;
// Open the file, passed from other method
File file = new File(System.getProperty("user.dir") + "/" + fileName + ".txt");
for(scanner = new Scanner(file); scanner.hasNext(); ) //loop through lines in file
{
++n;
String line = scanner.nextLine();
if(rand.nextInt(n) == 0)
tip = line;
}
return tip;
}
}