Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Very Basic Java input Problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Very Basic Java input Problem

    Okay first things first here was my assignment:
    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:
    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.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    	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");
    				}
    			}
    		}
     
    	}
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    coke32 (October 6th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Very Basic Java input Problem

    Quote Originally Posted by newbie View Post
    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.
    	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!

  5. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  6. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: Very Basic Java input Problem

    Quote Originally Posted by newbie View Post
    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?

  8. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Basic Math Expression Java Problem
    By andyluvskrissy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2011, 03:22 AM
  2. [SOLVED] Very basic code problem
    By frederick213 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 12:33 PM
  3. Basic Math Expression Java Problem
    By andyluvskrissy in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 02:46 PM
  4. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM
  5. JDBCrealm FORM based problem. But BASIC working perfectly alright!
    By salman4u in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 27th, 2010, 09:06 PM