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

Thread: Not Sure Whats Wrong

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Not Sure Whats Wrong

    import java.util.*;
     
    public class Main
    {
    	public static boolean isPart;
    	public static String x[] = {"One", "Two", "Three", "Four", "Five"};
    	public static void main(String[] args)
    	{
     
    		isPartOfArray();
     
    	}
    	public static boolean isPartOfArray()
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.println("What is your name?");
    		String input = scan.nextLine();
    		System.out.println(input);
    		int i;
    		for(i = 0; i< 4; i++)
    		{
     
    		}
    		if(input.equals(x[i]))
    		{
    			System.out.println("Name recognized");
    			isPart = true;
    		}
    		else
    		{
    			System.out.println("Name not recognized");
    			isPart = false;
    		}
    		return isPart;
    	}
    }
    For some reason it's. always returning false even if an item from the array x is entered.


  2. #2
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Not Sure Whats Wrong

    You may want to look over the for loop you are using to access the array, see if you find something there
    Also, you should post your code within code tags, that makes it easier to see.
    English is not my native language (Typo alert).

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Not Sure Whats Wrong

    Why is the for() loop empty?

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

    Blykon (September 14th, 2014)

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Not Sure Whats Wrong

    So why not let the OP investigate and answer?

  6. #5
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Not Sure Whats Wrong

    Yeah, that was my plan, but then you basicly said it.
    Removed it.
    English is not my native language (Typo alert).

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

    Default Re: Not Sure Whats Wrong

    Quote Originally Posted by Time4Java View Post
    You may want to look over the for loop you are using to access the array, see if you find something there
    Also, you should post your code within code tags, that makes it easier to see.
    I have tweaked it And pondered on it for hours and cannot figure it out.

    Edit: Lmao nvm

    --- Update ---

    import java.util.*;
     
    public class Main
    {
    	public static boolean isPart;
    	public static String j;
    	public static String x[] = {"One", "Two", "Three", "Four", "Five"};
    	public static void main(String[] args)
    	{
     
    		isPartOfArray();
     
    	}
    	public static boolean isPartOfArray()
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.println("What is your name?");
    		String input = scan.nextLine();
    		System.out.println(input);
    		int i;
    		for(i = 0; i< 4; i++)
    		{
    			String j = x[i];
    		}
    		if(input.equals(j))
    		{
    			System.out.println("Name recognized");
    			isPart = true;
    		}
    		else
    		{
    			System.out.println("Name not recognized");
    			isPart = false;
    		}
    		return isPart;
    	}
    }
    Still not working.

  8. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Not Sure Whats Wrong

    The curly brackets { and } have a meaning in java. The way you use them is the source of your problem. You have misplaced the brackets for your loop and thus your program does not do what you would think it does.

  9. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not Sure Whats Wrong

    I'm not seeing any place where the curley brackets are used incorrectly. I'm guessing you are talking about a scope issue?

  10. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Not Sure Whats Wrong

    Yes exactly.
    You have 5 items in your array. You have your loop run from 0 to 3 which is 4 indices. But then you only check for the fourth item in your if-condition. This happens because the if-then-else is not within the for-loop. They are both seperated and dont interact with each other at all. This is why Greg pointed out that your loop is actually empty and doesnt do anything.

  11. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not Sure Whats Wrong

    I actually had that initially but I removed it from inside the for loop because I was getting an exception and I tried to see if it would help removing it from there.

    --- Update ---

    I actually had that initially but I removed it from inside the for loop because I was getting an exception and I tried to see if it would help removing it from there.

  12. #11
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Not Sure Whats Wrong

    When you use a for loop to access an array, what you are really doing is this:
    i = 0; i < 4; i ++
    So the first time the loop goes around it accesses the first element of the array, beeing 0, then it runs the loop again to access the next element and so on.
    So the accessing part does need to happen inside that loop

    Good luck man
    English is not my native language (Typo alert).

  13. #12
    Junior Member
    Join Date
    Sep 2014
    Location
    Goulds, FL
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Not Sure Whats Wrong

    Can someone help me with my bank account program? I finished the code, but I want to make sure everything is correct.
    Attached Files Attached Files

  14. #13
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Not Sure Whats Wrong

    Quote Originally Posted by ChristianBethel View Post
    Can someone help me with my bank account program? I finished the code, but I want to make sure everything is correct.
    Please make a seperate thread for your question, and put your code in code tags, not in a text file.
    English is not my native language (Typo alert).

  15. #14
    Junior Member
    Join Date
    Sep 2014
    Location
    Goulds, FL
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Not Sure Whats Wrong

    My apologies. How do I do that?

  16. #15
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Not Sure Whats Wrong

    Quote Originally Posted by ChristianBethel View Post
    My apologies. How do I do that?
    Topside of the forum you see: Ask a question. That should show you the way, ,)
    English is not my native language (Typo alert).

Similar Threads

  1. whats wrong
    By Jdino23 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2014, 03:42 PM
  2. Whats wrong here??
    By cbplayer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2013, 02:19 PM
  3. WHATS WRONG
    By subodhkr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 8th, 2012, 07:47 AM
  4. Whats wrong?
    By help2 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2012, 04:22 AM
  5. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM