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 7 of 7

Thread: Need help with nesting if statements! (Scrub) EDITED

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need help with nesting if statements! (Scrub) EDITED

    So im doing an assignment with the following requirements:

    The program prompts the user to enter a valid three digit
    double literal, and checks to make sure the input works.

    Requirements:
    1. Consists of exactly three of the following characters: +, -, . (decimal point), and 0 through 9.
    2. The + or - character may only appear (once) as the first character.
    3. The . (decimal point) character must appear exactly once,as either the first, second, or third character.
    4. All other characters must be the 0 through 9 characters

    EDIT:


    Heres what i currently have.

    public class Project04 
    {
     
    	public static void main(String[] args) 
    	{
    		Scanner stdIn = new Scanner(System.in);
     
    		char char0;
    		char char1;
    		char char2;
    		String DL; //Double Literal
     
    		System.out.println("Please enter a valid (3 character) double literal :");
    		DL = stdIn.nextLine();
     
    		char0 = DL.charAt(0);
    		char1 = DL.charAt(1);
    		char2 = DL.charAt(2);
     
    		if (char0 == '+' || char0 == '-')
    		{
    			if (char1 == '.');
    			{
    				if ('0' <= char2 && char2 <= '9')
    				{
    					System.out.println(DL + " Is a valid 3 character double literal");
    				}
    			}
     
    		}
    		else if ('0' <= char1 && char1 <= '9');
    		{
    			if (char2 == '.');
    			{
    				System.out.println( DL + " Is a valid 3 character double literal");
    			}
    		}
    		if (char0 == '.');
    		{
    			if ('0' <= char1 && char1 <= '9');
    			{
    				if ('0' <= char2 && char2 <= '9');
    				{
    					System.out.println(DL + " Is a valid 3 character double literal");
    				}
    			}
     
    		}
     
     
     
     
     
    	}	
     
    }

    The problem im having is that the code is not checking everything properly. To my understanding the first IF should be checking for a + or -, then if it has a decimal, then if the last char is between 0 and 9, the ELSE checking essentially the same thing except the last 2 chars are switched.The second IF should be checking if the first char is a decimal, and if it is it checks if the next 2 chars are between 0-9.

    However, when i run my program, i can type in even "..." and it still says " ... is a valid 3 character double literal.

    Do i have to nest these two IF trees together?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Need help with nesting if statements! (Scrub)

    Please post the code here on the forum. Be sure to wrap it in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with nesting if statements! (Scrub) EDITED

    I edited the first post

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Need help with nesting if statements! (Scrub) EDITED

    What characters are valid as the first character of the String? The if/else if statements could pick those up first. Then the nested if statements could look at what were valid following characters.

    The requirements for the code should be posted as comments in the code so you can look at them as you write the code.

    You need to use the -Xlint compiler option to get important messages about the code:
    D:\Java\jdk1.7.0.7\bin\javac.exe -cp . -Xlint Project04.java
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with nesting if statements! (Scrub) EDITED

    Sorry, in my actual code i do have the requirements listed but i only listed what i thought was the important section of code, since i already gave the requirements in the thread.

    To answer your question, +,-, . , and 0-9 are all valid as char0, but + and - are not valid for char1 or char2.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Need help with nesting if statements! (Scrub) EDITED

    +,-, . , and 0-9 are all valid as char0
    Does the code test for those first?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    48
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Need help with nesting if statements! (Scrub) EDITED

    From the looks of it you were very close.

    There are multiple ways you could go at this:

    • Your way, test the individual chars of the string literal for certain situations

      or

    • See if the string contains any character outside the allowed characters, then check to see if it has a decimal (since they all must), see if it has a + or - sign and if so, check that it's at the front. If it fails any of those tests return not a valid string, if it passes there you have it.


    Post back for questions

Similar Threads

  1. Help With Nesting Simple If-Else Loops
    By Sephyncloud in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 10th, 2013, 05:56 PM
  2. [SOLVED] If and else if statements???
    By skeptile2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 5th, 2013, 12:44 AM
  3. If Statements help
    By cowsquad in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2013, 05:41 AM
  4. Error In Printing. Perhaps a nesting problem
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 23rd, 2011, 07:46 AM
  5. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM