Need help - Here is an assignment I've been working on for two days.
Having problems with this assignment:
Code:
public class StringAssignment {
/**
* Look through every character of the String s.
* The string may begin with a '-' but does not need to
* Then, if there are only 0-9 and at most 1 '.' then s
* is a number, and return true
*
*
* @param s, the string to be tested
* @return true if is a number, or false otherwise
*/
public static boolean isStringANumber(String s){
//s.length() tells you how many characters are in the file
//s.charAt(index) gives you the character at spot index
//you can compare characters with the ==, for example if (c == '0')
//If the character is not a 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or . it is not a number
//If if is a . and there has already been 1 period in the file, it is not a number
//You can know early if it is not a number, but only after
//checking the entire string can you know if it is a number
// Integer x initialized.
int x = 0;
// For loop counting the entire s.length.
for (int i = 0; i < s.length(); i++){
// c becomes whatever s.charAt is.
char c = s.charAt(i);
//Finds first period then counts x up one.
if (c == '.'){
x++;
//If more than one period is found returns false.
if (x >= 2){
}
return false;
}
//Checks if c isn't the number 0 - 9 and returns false if it isn't.
if ( c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' ){
return false;
}
}
//everything else is true.
return true;
}
The result I'm getting from the grader I'm provided with reads this:
Testing isStringANumber
For some reason, -99999 is not being returned as a valid number
Problem with integers
For some reason, -99999.9 is not being returned as a valid number
Problem with doubles
In case it is helpful here is a copy of the grader that is used to test this file.
Code:
public static void main(String[] args) {
int points = 100;
int deduct = 100 / 6;
boolean b;
String answer;
System.out.println("Testing isStringANumber");
// integers
try {
for (int i = -99999; i < 99999; i++) {
b = StringAssignment.isStringANumber("" + i);
if (b == false) {
System.out.println("For some reason, " + i
+ " is not being returned as a valid number");
throw new Exception();
}
}
} catch (Exception e) {
System.out.println("\tProblem with integers");
points -= deduct;
}
// doubles
try {
for (double i = -99999.9; i < 99999.9; i += 1.9) {
b = StringAssignment.isStringANumber("" + i);
if (b == false) {
System.out.println("For some reason, " + i
+ " is not being returned as a valid number");
throw new Exception();
}
}
b = StringAssignment.isStringANumber(".5");
if (b == false) {
System.out
.println("For some reason, .5 is not being returned as a valid number");
throw new Exception();
}
} catch (Exception e) {
System.out.println("\tProblem with doubles");
points -= deduct;
}
// Invalids
try {
String[] invalidStrings = { "a", "-567a", "567n", "127.0.0.1",
".5a", "7.7.", ".77.", "{}", "Hello World", "---",
"a908.3", "5670'", "+=", "$$$", "123.456)5567",
"435-555-5555" };
for (int i = 0; i < invalidStrings.length; i++) {
b = StringAssignment.isStringANumber(invalidStrings[i]);
if (b == true) {
System.out.println("For some reason, " + invalidStrings[i]
+ " is being returned as a valid number");
throw new Exception();
}
}
} catch (Exception e) {
System.out.println("\tProblem with invalid strings");
points -= deduct;
}
if (points < 5) {
points = 0;
}
System.out.println("Total points " + points);
}
}
Any help would be appreciated. Thanks in advance.