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

Thread: Looping Question (newbie)

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looping Question (newbie)

    Hello, I am new in this forum, hope you guys can help me with my problem.

    Our professor gave us a take home quiz and I don't know what to do anymore after several attempts in doing the program. I have done 3 programs already and all of them was gone crazy.

    Would you guys mind help me in the program.

    OUTPUT:

    Enter a letter: J
    Enter a letter: A
    Enter a letter: V
    Enter a letter: E
    Enter a letter: C
    Enter a letter: J
    Enter a letter: A
    Enter a letter: V
    Enter a letter: A

    J-A-V-A formed!

    As you can see the word "JAVA" is formed from the 6th line to the last.

    The program goes like this "It will continue to loop as long as the word 'JAVA' is not formed" and when the word 'JAVA' is formed it will display "J-A-V-A formed!".

    Thanks a lot!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Looping Question (newbie)

    Ok, well since its a quiz I'll help as little as possible.

    So, lets walk through this. I guess my first question is: will it always stop at JAVA or is there other test cases where it will stop on a different user-specifiied word?

    If it always stops at JAVA, then this is easy. Tell me if you're allowed to do this:
    There is a method in the String class called indexOf(String value). This searches the string and returns the index value of the first occurance of the given value in the String. If it cant find the string, it returns -1.
    So, what you might be able to do is grab each letter and put it in one long String. Then use the String.indexOf("JAVA") method to get the index of the first occurance of JAVA. If it finds JAVA, presto.
    Last edited by aussiemcgr; July 19th, 2010 at 01:29 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping Question (newbie)

    The program needs to keep reading until it gets a "J"
    Then it MUST read an "A"
    if not an A it starts over
    Continue getting the next two letters: V & A using the same kind of logic

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question (newbie)

    @aussiemcgr
    In our situation, it will only stop if the word JAVA is formed.

    indexOf(String value) - I don't know this function yet because I think it is on more complex programming and I think that our class were only at the basic.

    Would you mind provide me a source code for this program, then I'll try to analyze it so I can explain it to my classmates.

    @Norm
    You got the logic sir.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping Question (newbie)

    You got the logic
    Is that a question or a statement?

    I thought I gave you a start on the logic. You'll need to fill it in.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Looping Question (newbie)

    @aussiemcgr
    In our situation, it will only stop if the word JAVA is formed.

    indexOf(String value) - I don't know this function yet because I think it is on more complex programming and I think that our class were only at the basic.

    Would you mind provide me a source code for this program, then I'll try to analyze it so I can explain it to my classmates.

    Sure, but please dont use this as your quiz answer...

    I'll show you how it works. Very simple. I'm going to assume you are using Scanner (despite how I hate it).

    import java.util.Scanner;
     
    public class Test
    {
     
        public static void main(String[] arguments)
        {
    		Scanner sc = new Scanner(System.in);
    		String temp = "";
    		while(true)
    		{
    			System.out.print("Enter a letter: ");
    			String value = sc.nextLine();
    			temp+=value;
    			if(temp.indexOf("JAVA")!=-1)
    			{
    				System.out.println("J-A-V-A formed!");
    				break;
    			}
    		}
       	}
    }

    I've actually never used that version of indexOf before (with a string sent to it), its kind of cool how it works.

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question (newbie)

    @Norm
    It is a statement.

    Hmm, I'll try to fill it.

    Enter a letter: J
    Enter a letter: A
    Enter a letter: C

    First, I enter "J" followed by "A" which is to formed JAVA but I enter a "C" on the third which will destroy the sequence of JAVA so we need to start all over again until we formed a JAVA word.

    Enter a letter: J
    Enter a letter: A
    Enter a letter: V
    Enter a letter: A

    So the output will become like this:

    Enter a letter: J
    Enter a letter: A
    Enter a letter: C
    Enter a letter: J
    Enter a letter: A
    Enter a letter: V
    Enter a letter: A

  8. #8
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question (newbie)

    Quote Originally Posted by aussiemcgr View Post
    Sure, but please dont use this as your quiz answer...

    I'll show you how it works. Very simple. I'm going to assume you are using Scanner (despite how I hate it).

    import java.util.Scanner;
     
    public class Test
    {
     
        public static void main(String[] arguments)
        {
    		Scanner sc = new Scanner(System.in);
    		String temp = "";
    		while(true)
    		{
    			System.out.print("Enter a letter: ");
    			String value = sc.nextLine();
    			temp+=value;
    			if(temp.indexOf("JAVA")!=-1)
    			{
    				System.out.println("J-A-V-A formed!");
    				break;
    			}
    		}
       	}
    }

    I've actually never used that version of indexOf before (with a string sent to it), its kind of cool how it works.
    I'm not familiar with Scanner but I think it is the same as BufferedReader?

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Looping Question (newbie)

    Quote Originally Posted by xdrechsler View Post
    @Norm
    It is a statement.

    Hmm, I'll try to fill it.

    Enter a letter: J
    Enter a letter: A
    Enter a letter: C

    First, I enter "J" followed by "A" which is to formed JAVA but I enter a "C" on the third which will destroy the sequence of JAVA so we need to start all over again until we formed a JAVA word.

    Enter a letter: J
    Enter a letter: A
    Enter a letter: V
    Enter a letter: A

    So the output will become like this:

    Enter a letter: J
    Enter a letter: A
    Enter a letter: C
    Enter a letter: J
    Enter a letter: A
    Enter a letter: V
    Enter a letter: A
    Thats about what he's saying. And thats probably the way your teacher/professor wants you to do it. The most important thing to remember when doing this is that you have to store the strings of the 4 most recent inputs so you can see if they form the word.

    I'm not familiar with Scanner but I think it is the same as BufferedReader?
    Dunno, never used BufferedReader to do console programs. The only time I used the Buffered stuff was when I was importing pictures and other data files from within a program's jar.
    Last edited by aussiemcgr; July 19th, 2010 at 02:37 PM.

  10. #10
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question (newbie)

    Dunno, never used BufferedReader to do console programs. The only time I used the Buffered stuff was when I was importing pictures and other data files from within a program's jar.
    Its okay, I'm after the logic. I'll just ask my professor about it.

    Can I ask some questions?
    temp+=value; -> this is to store the value of the 4 strings to be formed right?

    I don't get the -1 in the if statement. Could you explain how it works.
    Last edited by xdrechsler; July 19th, 2010 at 02:56 PM.

  11. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Looping Question (newbie)

    Quote Originally Posted by xdrechsler View Post
    Its okay, I'm after the logic here. I'll just ask my professor about it.

    Can I ask some questions?
    temp+=value; -> this is to store the value of the 4 strings to be formed right?

    I don't get the -1 in the if statement. Could you explain how it works.
    Actually, temp+=value; is the same as saying temp = temp+value. Since we are doing it with Strings, we are saying add value to the end of temp. But, we never clear temp for this example so it just keeps adding on the to the string, which is fine since we are using the indexOf method. However, doing it the other way, you want to store the strings in their own individual strings because you need to erase the previous, shift the values down, and read the new one in.

    So, as the loop goes, here is what temp equals:
    temp = J
    temp = JA
    temp = JAV
    temp = JAVE
    temp = JAVEC
    temp = JAVECJ
    temp = JAVECJA
    temp = JAVECJAV
    temp = JAVECJAVA

    As for the -1. String.indexOf(String value) returns an int of the index of the start of the word give in value. If it cannot find the value, it returns -1. So, by saying: if(temp.indexOf("JAVA")!=-1) you are checking to see if String.indexOf(String value) found value in the string. If it does find value, it will return a value other than -1. So we only want to stop the loop when String.indexOf(String value) returns something other than -1.

    If that doesnt make sense, here is the API entry for this method in the String class. The API is your BEST FRIEND, you will be referencing it every time you program to look for methods and parameters. Whenever I code, I always have the web browser open to the API.

    indexOf
    public int indexOf(String str)Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
    this.startsWith(str, k)
    is true.

    Parameters:
    str - any string.
    Returns:
    if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
    Last edited by aussiemcgr; July 19th, 2010 at 03:04 PM.

  12. The Following User Says Thank You to aussiemcgr For This Useful Post:

    JavaPF (July 20th, 2010)

  13. #12
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question (newbie)

    Quote Originally Posted by aussiemcgr View Post
    Actually, temp+=value; is the same as saying temp = temp+value. Since we are doing it with Strings, we are saying add value to the end of temp. But, we never clear temp for this example so it just keeps adding on the to the string, which is fine since we are using the indexOf method. However, doing it the other way, you want to store the strings in their own individual strings because you need to erase the previous, shift the values down, and read the new one in.

    So, as the loop goes, here is what temp equals:
    temp = J
    temp = JA
    temp = JAV
    temp = JAVE
    temp = JAVEC
    temp = JAVECJ
    temp = JAVECJA
    temp = JAVECJAV
    temp = JAVECJAVA

    As for the -1. String.indexOf(String value) returns an int of the index of the start of the word give in value. If it cannot find the value, it returns -1. So, by saying: if(temp.indexOf("JAVA")!=-1) you are checking to see if String.indexOf(String value) found value in the string. If it does find value, it will return a value other than -1. So we only want to stop the loop when String.indexOf(String value) returns something other than -1.

    If that doesnt make sense, here is the API entry for this method in the String class. The API is your BEST FRIEND, you will be referencing it every time you program to look for methods and parameters. Whenever I code, I always have the web browser open to the API.
    That clears it. I thought that the use of the indexOf("JAVA")!=-1 is to scan every 4- letter word from the start and when it does find the word "JAVA", there will be some values that is not equal to -1 that makes the if-statement satisfied and display the "J-A-V-A formed!".

    Thank you for explaining it to me very well, I learned a lot from you.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping Question (newbie)

    the -1 in the if statement.
    Read the API doc for the indexOf() method to see what values it returns.

  15. #14
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Looping Question (newbie)

    Quote Originally Posted by xdrechsler View Post
    That clears it. I thought that the use of the indexOf("JAVA")!=-1 is to scan every 4- letter word from the start and when it does find the word "JAVA", there will be some values that is not equal to -1 that makes the if-statement satisfied and display the "J-A-V-A formed!".

    Thank you for explaining it to me very well, I learned a lot from you.
    Well, for informational purposes, using the last value of temp (JAVECJAVA), the method indexOf("JAVA") would return 5 I believe. Because that is the index of the start of the word JAVA in JAVECJAVA.

    Indexes of Strings are one the most important thing to learn. It took me a while to figure out how string indexing worked so here is how I figured it out.

    For this, think of the first row as the String and the second row and the indexes indicating the spaces between the letters (sorry about spacing, couldnt get the forum to post multiple spaces correctly).

    . J A V E C J A V A
    0 1 2 3 4 5 6 7 8 9

    So if you called String.substring(3,6), which returns the string of the indexes between the selected ranges, you would get ECJ. And if we called String.substring(5,9) (or String.substring(5) if you want from the index to the end) you would receive JAVA.

    Understanding the substring method and the indexing of strings is vital to understanding how to scan through a string or to cut off parts of strings.

Similar Threads

  1. Looping Question
    By miss confused in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 30th, 2010, 12:46 PM
  2. Looping object creation
    By Charlie in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2010, 11:12 AM
  3. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM
  4. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM