Intro to java hw assignment
Just kinda looking to brainstorm with people or get some good tips (looking to learn too not just post here for someone to do my hw). Remember I'm literally like only 2 weeks into java, we are just learning about if, else statements and putting them to use.Anyway, here's my hw assignment:
Quote:
Main topics: User input
Basic String Methods
Boolean Expressions
if & if - else Statements
Program Specification:
Write a Java program that does the following:
Prompts the user to input exactly three characters, which constitude a valid Double literal in Java.
Gets whatever the user enters and stores it into a String variable, using the the Scanner's .nextLine() method.
Determines if the input string is indeed a valid Double literal of length three.
Displays this determination to the user in a reasonable report format.
Grading:
Performance Indicator [1] [2] [3]
Readability and documentation1 2 2
Use of conditional operators 1 2 2
Functional requirements 2 3 4
Efficiency 1 2 2
Sample run(s):
Please enter a valid (3 character) double literal : 123
123 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : 002
002 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : +45
+45 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : -45
-45 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : 4.5
4.5 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : 0.1
0.1 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : 45.
45. is a valid (3 character) double literal
Please enter a valid (3 character) double literal : +.1
+.1 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : -.1
-.1 is a valid (3 character) double literal
I know i'll ask the user to enter the double literal my question is how do i go about making sure its valid input. do i just if statements for each individual char spot? like if (input.charAt(0) >= 0 $$ <= 9) then move to charat(1) ect ect cause there is 8 possible combinations they could have in a sense, (+,-) (0-9) (.) if they start with a +,- they're is going to be followed by a int or a '.' , if they start with a '.' then the rest have to be int's obviously cause you can't go .+4 thats not a valid double literal i don't think. Just thinking while i type lol... any pointers are helpful. Am i sort of on the right track?
PS: if i sound like i don't have a clue to what i'm talking about... its cause i don't.. lol
Re: Intro to java hw assignment
If you are only two weeks into Java, then you should be learning about concepts and not error checking. Did your teacher tell you to assume correct input? You shouldn't be learning about handling Exceptions yet.
In any case, you can turn a String into a double via Double.parseDouble(stringToken) static method. If you receive no errors and the stringToken.length() == 3, then it is a valid double of length 3 characters.