11 Replies - 1992 Views - Last Post: 28 February 2010 - 10:56 AM Rate Topic: -----

#1 zutigard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-February 10

infinite loop problem

Posted 27 February 2010 - 08:16 PM

hello,
i am very new to programming and i'm having difficulty understanding how to set up loops. i am attempting to figure out how to take a line of text and a. count the chars, b. count the tokens, c. determine the longest token length, and d. print the frequency of each letter. i wrote the nested for loops first and that part worked (part d.) . the length of the string includes the whitespaces and i'm not sure how to subtract those (part a.). When i comment out everything but the nested for loops i am able to print the frequency of letters.

currently i'm just trying to figure out why i have an indefinite while loop.
import java.util.*;
public class Tokens {
	public static void main(String[]args){
		String input, token1 = "", token2 = "", longest = "";
		int c, x, count;
		
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter a line of text: ");
		input = keyboard.nextLine();
		input = input.trim();
		input = input.toUpperCase();
		int number = input.length();
		System.out.println("The line contains" + number + "letters.");
		x = 0;
		while(x < input.length()){
			if(input.charAt(x) == ' '){
				token1 = input.substring(0, x);
				token2 = input.substring(x + 1);	
			}
			if(token1.length() >= token2.length())
				longest = token1;
				else
					longest = token2;
			}
			++x;

		//System.out.println("The longest token is: " + (longest));
	

		for (c='A'; c<='Z'; c++){
		    count = 0;
		    for (int j=0; j<input.length(); j++){
		       if(input.charAt(j) == c)  count++;
		     }
		     if(count > 0)
		     System.out.println((char)c + " -- " + count);
	}
		
	}}


Is This A Good Question/Topic? 0
  • +

Replies To: infinite loop problem

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: infinite loop problem

Posted 27 February 2010 - 09:30 PM

You might want to check out the API for the String class for some useful methods. One of these is split(delim), which returns a String[] from the String based on the number of delim-seperated tokens. So if you want to separate by whitespace, simply say String[] tokens = stringName.split(" ");. This way, it is very simple to count the number of tokens and find the longest token.

As for the frequency of the letters, keep in mind that 'A' is ASCII 65 (so 65 as an int) and there are 26 letters in the alphabet. Also, if you say int x = 'A';, x will be equal to 65, so this means that chars can be implicitly converted to ints. You can use this to with an array. Hint- 26 letters, array of 26 elements.

Knowing these, you might want to try redoing your program to incorporate these suggestions, as they will make your life a lot easier. Good luck! :)
Was This Post Helpful? 1
  • +
  • -

#3 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: infinite loop problem

Posted 27 February 2010 - 10:34 PM

Deffinately use the comments above, will make it so much easier for you. If you want to know why you have an infinite loop, its because your incrementing x outside of your while loop. I guess you over/under counted the } Look
while(x < input.length()){ //OPEN WHILE
                        if(input.charAt(x) == ' '){ //OPEN IF
                                token1 = input.substring(0, x);
                                token2 = input.substring(x + 1);        
                        }//CLOSE IF
                        if(token1.length() >= token2.length()) 
                                longest = token1;
                                else
                                        longest = token2;
                        }//CLOSE WHILE
                        ++x;

Was This Post Helpful? 1
  • +
  • -

#4 zutigard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-February 10

Re: infinite loop problem

Posted 28 February 2010 - 07:38 AM

alright well after moving the ++x into the while loop i was able to stop the indefinite loop. i still don't understand String[] input.split(" ");... when i added that I got "[Ljava.lang.String;@14318bb" returned, and i have no idea what to do with this.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: infinite loop problem

Posted 28 February 2010 - 09:06 AM

The split() method returns an array of Strings separated by whitespace. I think the API explains it well enough, it is an invaluable resource:
http://java.sun.com/...va.lang.String)

Also, you getting that output because you are printing Objects that have no overridden toString() method. Please show you updated code.
Was This Post Helpful? 0
  • +
  • -

#6 zutigard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-February 10

Re: infinite loop problem

Posted 28 February 2010 - 09:54 AM

String[] tokens = input.split(" ");
tokens = toString(tokens);
System.out.println(tokens); 


i don't understand what is really being said here... when this is added in the code the return is null.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: infinite loop problem

Posted 28 February 2010 - 09:58 AM

Oh, I think you want to print each value of the array. For example, this is how you would get the tokens and print them.

String[] tokens = input.split(" ");
for (String s : tokens) 
    System.out.println(s);



That is called a for each loop and reads: "for each String s in the array tokens, print s".
Was This Post Helpful? 1
  • +
  • -

#8 zutigard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-February 10

Re: infinite loop problem

Posted 28 February 2010 - 10:07 AM

well that does print them, thanks. now that i have them in tokens i need to compare their lengh though. do i need to create two variable to hold these tokens to compare their char lengths? i initial tried this:
 		x = 0;
		/*while(x < input.length()){
			if(input.charAt(x) == ' '){
				token1 = input.substring(0, x);
				token2 = input.substring(x + 1);	
			}
			if(token1.length() >= token2.length())
				longest = token1;
				else
					longest = token2;
			
			++x;
		}*/

i got this from an example in my textbook that seperated two a string into tokens.. i thought i would be able to seperate as many tokens as were entered but it doesn't seem to work at all
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: infinite loop problem

Posted 28 February 2010 - 10:14 AM

Well, actually, with the array of tokens, you can determine the largest of any number of tokens...

String[] tokens = input.split(" ");

// Set the initial longest to the first index.
int longest = tokens[0].length();
String longStr = tokens[0];

for (int i = 1; i < tokens.length; i++) {

    // However, if another index is longer, then set that one.
    if (tokens[i].length() > longest) {
        longest = tokens[i].length();
        longStr = tokens[i];
    }
}

// longStr is now the longest string of all the tokens


Was This Post Helpful? 1
  • +
  • -

#10 zutigard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-February 10

Re: infinite loop problem

Posted 28 February 2010 - 10:38 AM

import java.util.*;
public class Zell_Tokens {
	public static void main(String[]args){
		String input; 
		int c, x, count;
		
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter a line of text: ");
		input = keyboard.nextLine();
		input = input.trim();
		input = input.toUpperCase();
		String[] tokens = input.split(" ");
		for (String s : tokens) {
		
			int longest = tokens[0].length();
			String longStr = tokens[0];

			for (int i = 1; i < tokens.length; i++) {
			
			    if (tokens[i].length() > longest) {
			        longest = tokens[i].length();
			        longStr = tokens[i];
			    }
			    System.out.println("the longest string is"+longStr);
			}

		for (c='A'; c<='Z'; c++){
		    count = 0;
		    for (int j=0; j<input.length(); j++){
		       if(input.charAt(j) == c)  count++;
		     }
		     if(count > 0)
		     System.out.println((char)c + " -- " + count);
	}
		
	}
	}}


yikes! this results in chaos...
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: infinite loop problem

Posted 28 February 2010 - 10:48 AM

Change this:
for (String s : tokens) {
                
                        int longest = tokens[0].length();
                        String longStr = tokens[0];

                        for (int i = 1; i < tokens.length; i++) {
                        
                            if (tokens[i].length() > longest) {
                                longest = tokens[i].length();
                                longStr = tokens[i];
                            }
                            System.out.println("the longest string is"+longStr);
                        }



To this:
             
                        int longest = tokens[0].length();
                        String longStr = tokens[0];

                        for (int i = 1; i < tokens.length; i++) {
                        
                            if (tokens[i].length() > longest) {
                                longest = tokens[i].length();
                                longStr = tokens[i];
                            }
                            System.out.println("the longest string is"+longStr);
                        }



because you don't a foreach loop AND and a for loop.
Was This Post Helpful? 1
  • +
  • -

#12 zutigard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-February 10

Re: infinite loop problem

Posted 28 February 2010 - 10:56 AM

thank you very much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1