-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberReader.java
101 lines (81 loc) · 3.19 KB
/
NumberReader.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JOptionPane;
//this class provides a scanner functions for the main program
public class NumberReader {
//scan integers array as a string and returns int array
public static int[] readIntNumsFromCommandLine() {
String inputString= JOptionPane.showInputDialog("Please enter a set of degrees for your first polynom:\n(note that you must choose a similar number of coefficients)\n");
String[] inputParsed = inputString.split(" ");
int[] intArray = new int[inputParsed.length], temp;
int i=0;
for(int j=0;j<inputParsed.length;i++,j++){
try{
intArray[i] = Integer.parseInt(inputParsed[j]);
}catch(NumberFormatException e){
i--;
}
}
temp = new int[i];
temp = Arrays.copyOf(intArray,i);
return temp;
}
//scan doubles array as a string and returns double array
public static double[] readDoubleNumsFromCommandLine() {
String inputString= JOptionPane.showInputDialog("Please enter a set of coefficients for your first polynom:\n(Make sure it match to the number of degrees.)");
String[] inputParsed = inputString.split(" ");
double[] doubleArray = new double[inputParsed.length], temp;
int i=0;
for(int j=0;j<inputParsed.length;i++,j++){
try{
doubleArray[i] = Double.parseDouble(inputParsed[j]);
}catch(NumberFormatException e){
i--;
}
}
temp = new double[i];
temp = Arrays.copyOf(doubleArray,i);
return temp;
}
//get polynom values from user
public static Polynom polynomReader () {
boolean tryAgainFlag = true;
while(tryAgainFlag) {
try { //input the sets of arrays
//the user provide 2 sets of 2 arrays to build the polynoms.
int[] arrIntNumbers1, arrIntNumbers2;
double[] arrDoubleNumbers1, arrDoubleNumbers2;
arrIntNumbers1 = NumberReader.readIntNumsFromCommandLine();
arrDoubleNumbers1 = NumberReader.readDoubleNumsFromCommandLine();
return new Polynom(arrIntNumbers1,arrDoubleNumbers1);
//catches inside exception that constructor has thrown
}catch(ArrayIndexOutOfBoundsException error) {
if(NumberReader.readYesOrNo())
tryAgainFlag=true;
else tryAgainFlag = false; //except "yes" or "no" as indication to start over.
}
}
return null;
}
//scan yes or no as a string and return boolean indicator
public static boolean readYesOrNo() {
String inputString;
while(true) {
inputString = JOptionPane.showInputDialog("do you want to try again? (yes/no)");
inputString.strip();
if(inputString.equals("yes"))
{
JOptionPane.showMessageDialog(null, "Starting over!");
return true;
}
if(inputString.equals("no"))
{
JOptionPane.showMessageDialog(null, "Terminating program, goodbye!");
return false;
}
else {
JOptionPane.showMessageDialog(null, "'"+inputString+"' is not a valid request, please type again .");
}
}
}
}