# Looping Question (newbie)

• July 19th, 2010, 11:35 AM
xdrechsler
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!
• July 19th, 2010, 12:07 PM
aussiemcgr
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.
• July 19th, 2010, 12:28 PM
Norm
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
• July 19th, 2010, 02:14 PM
xdrechsler
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.
• July 19th, 2010, 02:15 PM
Norm
Re: Looping Question (newbie)
Quote:

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.
• July 19th, 2010, 02:22 PM
aussiemcgr
Re: Looping Question (newbie)
Quote:

@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.

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

Code java:

```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.
• July 19th, 2010, 02:25 PM
xdrechsler
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
• July 19th, 2010, 02:32 PM
xdrechsler
Re: Looping Question (newbie)
Quote:

Originally Posted by aussiemcgr

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

Code java:

```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?
• July 19th, 2010, 02:33 PM
aussiemcgr
Re: Looping Question (newbie)
Quote:

Originally Posted by xdrechsler
@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.

Quote:

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.
• July 19th, 2010, 02:40 PM
xdrechsler
Re: Looping Question (newbie)
Quote:

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.

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.
• July 19th, 2010, 03:00 PM
aussiemcgr
Re: Looping Question (newbie)
Quote:

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

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.

Quote:

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.
• July 19th, 2010, 08:31 PM
xdrechsler
Re: Looping Question (newbie)
Quote:

Originally Posted by aussiemcgr
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.
• July 19th, 2010, 08:38 PM
Norm
Re: Looping Question (newbie)
Quote:

the -1 in the if statement.
Read the API doc for the indexOf() method to see what values it returns.
• July 19th, 2010, 09:12 PM
aussiemcgr
Re: Looping Question (newbie)
Quote:

Originally Posted by xdrechsler
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.