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);
}
}}

New Topic/Question
Reply



MultiQuote







|