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

Thread: a problem with a code... (for loop and an "if" nested..)

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

    Default a problem with a code... (for loop and an "if" nested..)

    the purpose of the code is to ask the user to input a string.
    if there is an "a" in it - the place of it in the string will be shown. if not - there will be a line "no a in the string".

    the problem is that i wrote the sentence "no a in the string" in the loop, and therefore it appears a few times and not only once....

    how can i write it out of the loop? put the loop in another condition?
    here's the code:


    import java.util.Scanner;

    public class Tirgul {

    public static void main(String[] args) {

    Scanner s = new Scanner(System.in);

    System.out.println ("enter a string");
    String str = s.nextLine();
    int i = str.length();
    int b;


    for (b=0; i > b;++b) {
    if (str.charAt(b) == 'a')
    System.out.println ("a is in the " + (b+1) + " place");

    else

    System.out.println ("no a in the string");
    }
    }
    }
    Last edited by kobi1988; October 13th, 2011 at 12:48 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: a problem with a code... (for loop and an "if" nested..)

    Set a boolean to false before the loop. Set the boolean to true inside the loop. After the loop, only do the printing if the boolean is true.

    You also might want to check out the String API. A lot of this work has been done for you already.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: a problem with a code... (for loop and an "if" nested..)

    ummmm....
    can you give me an example to simplify it...?
    i began studying this just yesterday... :/

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: a problem with a code... (for loop and an "if" nested..)

    Quote Originally Posted by kobi1988 View Post
    ummmm....
    can you give me an example to simplify it...?
    i began studying this just yesterday... :/
    What have you tried? What about my explanation did you not understand?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  3. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  5. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM