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: else if statement

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default else if statement

    this is my code and i need to add in

    System.out.println("Unknown type: " + result[1]);

    when i add it after the first if statement it prints it out 8 times instead of once.

    so my question is where do i add it so it prints it once
    import java.util.Scanner;
     
    class lab5{
     
            public static void main(String[] args){
     
                    String[] type = {"byte", "short", "int", "long", "float", "double", "Boolean", "char", "String" };
     
                    System.out.println("Enter a Java declaration followed by ; ");
                    Scanner s = new Scanner(System.in);
     
                    boolean validtype = false;
     
                    while(true){
                            String input = s.nextLine();
                            String[] result =input.split(" ");
     
                            for(int i=0;i<type.length;i++){
     
                                    if(type[i].equals(result[0])){
                                            System.out.println("Type is " + type[i]);
     
                                            if(result[1].substring(result[1].length()-1).equals(";")){
                                                    result[1]=result[1].substring(0,result[1].length()-1);  
                                                    System.out.println("Varible is " + result[1]);
     
                                            } else {System.out.println("Please enter again with ;");}
     
     
                                    } //end of first if statement
                                    validtype = true;
                            } //end of else for loop        
     
                            break;
                    } //end of while loop
            } //end of main method
    Last edited by Random1; April 10th, 2014 at 12:14 AM.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: else if statement

    What exactly are you trying to get accomplished? Your question isn't very clear.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: else if statement

    It prints out "Unknown type" 9 (not 8) times because there are 9 elements in the type array. This repeated printing is the result of:
    for (int i = 0; i < type.length; i++) {  // <== Iteration being done by this
     
        if (type[i].equals(result[0])) {  // <== Type checking here
            System.out.println("Type is " + type[i]);
     
            if (result[1].substring(result[1].length() - 1).equals(";")) {
                result[1] = result[1].substring(0, result[1].length() - 1);
                System.out.println("Varible is " + result[1]);
     
            }
            else {
                System.out.println("Please enter again with ;");
            }
     
        } // end of first if statement
        else {  // <== If you add "else" here
            System.out.println("Unknown type " + result[1]);  // <== If you print "Unknown type" here
        }
     
    } // end of else for loop  <== End of "for" loop body, return to start of loop until "i < type.length"
    In order for "Unknown type" to be printed out once, you can place the println statement after the for loop. E.g.,
    for (int i = 0; i < type.length; i++) {
        ...
    } // end of else for loop
     
    if (!validtype) {
        System.out.println("Unknown type " + result[1]);
    }
    However you'll also need to test the validtype variable, and so this variable will need to be set correctly. At the moment validtype is not set correctly:
    for (int i = 0; i < type.length; i++) {
     
        if (type[i].equals(result[0])) {  // <== checks if the extracted word is one of the types in the type array
            System.out.println("Type is " + type[i]);
            ...
        } // end of first if statement
        validtype = true;  // <== sets to true regardless of the above if check
    } // end of else for loop
    You can set validtype to true right after the first if check, i.e.,
    if (type[i].equals(result[0])) {
        validtype = true;  // <== Set to true here
        System.out.println("Type is " + type[i]);
        ...
    Note that after having done the above you'll avoid printing "Unknown type" multiple times, but the overall code logic is still flawed. You just need to test this by entering "int a = 1;" and you'll see that not everything is working as expected. I'd suggest that you add println statements for your variables at key points in your code to further understand what is really happening.

  4. The Following User Says Thank You to jashburn For This Useful Post:

    Random1 (April 10th, 2014)

  5. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: else if statement

    import java.util.Scanner;
     
    class lab5{
     
            public static void main(String[] args){
     
                    String[] type = {"byte", "short", "int", "long", "float", "double", "Boolean", "char", "String" };
     
                    System.out.println("Enter a Java declaration followed by ; ");
                    Scanner s = new Scanner(System.in);
     
                    boolean validtype = false;
     
                    while(true){
                            String input = s.nextLine();
                            String[] result =input.split(" ");
     
                            for(int i=0;i<type.length;i++){
     
                                    if(type[i].equals(result[0])){
                                            validtype = true;
                                            System.out.println("Type is " + type[i]);
     
                                            if(result[1].substring(result[1].length()-1).equals(";")){
                                                    result[1]=result[1].substring(0,result[1].length()-1);
                                                    System.out.println("Varible is " + result[1]);
     
                                            } else {System.out.println("Please enter again with ;");}
     
     
                                    } //end of first if statement
                                    validtype = true;
                            } //end of else for loop        
                            if (!validtype) {
                                System.out.println("Unknown Type " + result[1]);
                            } //end of if statement^
                            break;
                    } //end of while loop
            } //end of main method
    } //end of class

    this is what i got but it still isnt printing unknown type
    Last edited by Random1; April 10th, 2014 at 09:13 PM.

  6. #5
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: else if statement

    One easier way would be to simply add an else statement
    // Checks if your input matches this type
    if (type[i].equals(result[0]))) {
        System.out.println("Type is " + type[i]);
     
        if (result[1].substring(result[1].length() - 1).equals(";")) {
            result[1] = result[1].substring(0, result[1].length() - 1);
            System.out.println("Variable is " + result[1]);
        } else { 
            System.out.println("Please enter again with ;");
        }
    } else { // Type did not match, print out error
        System.out.println("Unknown type: " + result[0]);
    }

    Now after looking at your code a bit more, are you sure you need to loop through like this? If all your after is whether or not they entered in a type contained within the array, you can test that first rather than having all this logic inside your for loop.
    The way it's currently written, it will say it is an invalid type 8 times then once say that it was valid.

    What you want to think about doing is this.
    1. Get input from user
    2. Check if input is contained in your array
    3. Now print whether or not it is a valid type

  7. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: else if statement

    So I'm confused what to do then...all I need is to print the unknown type if the users input doesn't match that of the array

  8. #7
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: else if statement

    Let me try to help you learn to debug problems. The following is the same as the code you posted in post #4, but with additional println statements added to show what happens when you execute the program.
    import java.util.*;
     
    class lab5 {
     
        public static void main(String[] args) {
     
            String[] type = { "byte", "short", "int", "long", "float", "double", "Boolean", "char",
                "String" };
     
            System.out.println("Enter a Java declaration followed by ; ");
            Scanner s = new Scanner(System.in);
     
            boolean validtype = false;
     
            while (true) {
                String input = s.nextLine();
                String[] result = input.split(" ");
                System.out.println(">>> A) result = " + Arrays.deepToString(result));
                System.out.println(">>> B) result[0] = " + result[0]);
     
                for (int i = 0; i < type.length; i++) {
                    System.out.println(">>> C) type[" + i + "] = " + type[i]);
     
                    if (type[i].equals(result[0])) {
                        validtype = true;
                        System.out.println("Type is " + type[i]);
     
                        if (result[1].substring(result[1].length() - 1).equals(";")) {
                            result[1] = result[1].substring(0, result[1].length() - 1);
                            System.out.println("Varible is " + result[1]);
     
                        }
                        else {
                            System.out.println("Please enter again with ;");
                        }
     
                    } // end of first if statement
                    System.out.println(">>> D) validtype = " + validtype);
                    validtype = true;
     
                } // end of else for loop
     
                System.out.println(">>> E) validtype = " + validtype);
                if (!validtype) {
                    System.out.println("Unknown Type " + result[1]);
                } // end of if statement^
                break;
     
            } // end of while loop
        } // end of main method
    } // end of class
    Compile and run the above, and observe the ">>>" print outs. Try to figure out the reason it doesn't print "Unknown Type" when it is supposed to do so. Add additional println statements as necessary. Also, from the ">>>" print outs think how your code might be better structured to get the necessary results.

Similar Threads

  1. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  2. HELP: If-Else Statement
    By JennyDang in forum What's Wrong With My Code?
    Replies: 14
    Last Post: March 19th, 2013, 08:12 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM