Very Basic Java input Problem
Okay first things first here was my assignment:
Quote:
Due: Monday, October 10th, 2011.
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 documentation 1 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
secondly heres my code:
Code :
import java.util.Scanner;
public class Weekly03 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String literal;
Scanner stdIN = new Scanner(System.in);
System.out.print("Please enter a valid (3 character) double literal: ");
literal = stdIN.nextLine();
if (literal.charAt(0) >= 0 && literal.charAt(0) <= 9)
{
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9)
{ if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9 || literal.charAt(2) == '.')
System.out.println(literal + " is a valid double literal!");
else
System.out.println("Invalid input");
}
else if (literal.charAt(1) == '.')
{ if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9)
System.out.println(literal + " is a valid double literal!");
else
System.out.println("Invalid input");
}
else if (literal.charAt(0) == '+' || literal.charAt(0) == '-')
{
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9)
{ if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9 || literal.charAt(2) == '.')
System.out.println(literal + " is a valid double literal!");
else
System.out.println("Invalid input");
}
else if (literal.charAt(1) == '.')
{ if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9 )
System.out.println(literal + " is a valid double literal!");
else
System.out.println("Invalid input");
}
}
else if (literal.charAt(0) == '.')
{
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9)
{ if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9)
System.out.println(literal + " is a valid double literal!");
else
System.out.println("Invalid input");
}
else
System.out.println("Invalid input");
}
}
}
}
i thought i hit all possible valid combinations with my if, else if statements, yet when i run the program i get no output. it just is blank after i enter the possible entries. I am VERYY NEW to java this is like our 3rd assignment in class and I'm just looking for any help.
Re: Very Basic Java input Problem
Your problem is simply based on misinterpretations of how your if blocks are nested.
To avoid this in the future, always use braces to open and close each block.
Here I have simply added in all the braces for you, so if you follow them, you should notice that you haven't protected against all possible outcomes.
Code java:
public static void main(String[] args) throws ParseException {
Scanner stdIN = new Scanner(System.in);
System.out.print("Please enter a valid (3 character) double literal: ");
String literal = stdIN.nextLine();
if (literal.charAt(0) >= 0 && literal.charAt(0) <= 9) {
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9) {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9 || literal.charAt(2) == '.') {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else if (literal.charAt(1) == '.') {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9) {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else if (literal.charAt(0) == '+' || literal.charAt(0) == '-') {
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9) {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9 || literal.charAt(2) == '.') {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else if (literal.charAt(1) == '.') {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9) {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
}
} else if (literal.charAt(0) == '.') {
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9) {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9) {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else {
System.out.println("Invalid input");
}
}
}
}
Re: Very Basic Java input Problem
Quote:
Originally Posted by
newbie
Your problem is simply based on misinterpretations of how your if blocks are nested.
To avoid this in the future, always use braces to open and close each block.
Here I have simply added in all the braces for you, so if you follow them, you should notice that you haven't protected against all possible outcomes.
Code java:
public static void main(String[] args) throws ParseException {
Scanner stdIN = new Scanner(System.in);
System.out.print("Please enter a valid (3 character) double literal: ");
String literal = stdIN.nextLine();
if (literal.charAt(0) >= 0 && literal.charAt(0) <= 9) {
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9) {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9 || literal.charAt(2) == '.') {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else if (literal.charAt(1) == '.') {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9) {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else if (literal.charAt(0) == '+' || literal.charAt(0) == '-') {
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9) {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9 || literal.charAt(2) == '.') {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else if (literal.charAt(1) == '.') {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9) {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
}
} else if (literal.charAt(0) == '.') {
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9) {
if (literal.charAt(2) >= 0 && literal.charAt(2) <= 9) {
System.out.println(literal + " is a valid double literal!");
} else {
System.out.println("Invalid input");
}
} else {
System.out.println("Invalid input");
}
}
}
}
I see what you're saying as far as my blocks go(literally just learned about nesting the if statements), but when I copied your code in their the compiler gave me loads of errors. I got time but was wondering the proper way to block those, thanks again!
Re: Very Basic Java input Problem
I still can't seem to get it to have an output after user input. I've tried all kinds of boxing idk if I'm doing it right though :/. Still looking for help
Re: Very Basic Java input Problem
You would get errors as I only gave up the code for your main method.
For it to work, you obviously need to keep your original class declaration and imports.
The reason I nested them properly was so that you could see that you haven't guarded against NONE of the conditions happening.
Re: Very Basic Java input Problem
Quote:
Originally Posted by
newbie
You would get errors as I only gave up the code for your main method.
For it to work, you obviously need to keep your original class declaration and imports.
The reason I nested them properly was so that you could see that you haven't guarded against NONE of the conditions happening.
Yea I'm still struggling to get an output with this, even when using you're properly nested code, could it be because of my version of java on my macbook?
Re: Very Basic Java input Problem
The reason you don't get output is because you don't have else clauses to counter total mismatches.
The reason it seeks an else clause is because program logic is wrong.
When you compare chars with values, you're checking it against the following table:
http://www.asciitable.com/index/asciifull.gif
I.e if you have char 'A', and have a condition, if (myInt == 65), then that will return true, because the decimal ASCII value of 'A' is 65.
Re: Very Basic Java input Problem
I had the same assignment and I was having a similar problem.
My logic statements seemed to be right, and I wasn't getting any compiling errors, but, not matter what I did, it wouldn't output my println statement.
After going through a bunch of forums and my notes, I realized I didn't have the numbers in single quotes.
You're code says...
if (literal.charAt(1) >= 0 && literal.charAt(1) <= 9)
but it should say...
if (literal.charAt(1) >= '0' && literal.charAt(1) <= '9')
After I put the quotes into my logic statements, The program worked fine.
I hope this helps.