PDA

View Full Version : Java Help


ThRoNkA
03-10-2006, 10:10 PM
This is my java script for a program String.java and I don't why my values aren't adding up? If you could check and see why all my values always turn out ZERO and why that aren't adding. You'll be my eternal hero! Thanks.



import java.util.*;

public class String
{

public static void main(String[ ] args)
{

final String PHRASE1 = "money",
PHRASE2 = "thing",
PHRASE3 = "is",
SENTINEL = "####";

final boolean DEBUG_SW = true;

Scanner scan = new Scanner(System.in);

Character c1 = new Character(' ');
Character c2 = new Character(' ');

String userInput;

int numLowerStr = 0,
numUpperStr = 0,
numLowerTot = 0,
numUpperTot = 0,
numStr = 0,
keyPhrase1 = 0,
keyPhrase2 = 0,
keyPhrase3 = 0,
totLength = 0;
double avgLower,
avgUpper;
boolean sentCheck = true;


System.out.println("Enter a string (#### to end).");
userInput = scan.nextLine();
userInput = userInput.trim();

if (userInput.equals(SENTINEL))
sentCheck = false;



while(sentCheck)
{

if(userInput.indexOf(PHRASE1) != -1)
{
if(DEBUG_SW)
System.out.println(userInput);
keyPhrase1++;
}

if(userInput.indexOf(PHRASE2) != -1)
{
if(DEBUG_SW)
System.out.println(userInput);
keyPhrase2++;
}

if(userInput.indexOf(PHRASE3) != -1)
{
if(DEBUG_SW)
System.out.println(userInput);
keyPhrase3++;
}

for(int j = 0; j <= userInput.length(); j++)
{
if(c1.isLowerCase(j))
numLowerStr++;
}

for(int j = 0; j <= userInput.length(); j++)
{
if(c2.isUpperCase(j))
numUpperStr++;
}
totLength = totLength += userInput.length();
numLowerTot = numLowerTot += numLowerStr;
numUpperTot = numUpperTot += numUpperStr;

System.out.println("Enter another string (#### to end).");


userInput = scan.nextLine();
userInput = userInput.trim();

if (userInput.equals(SENTINEL))
sentCheck = false;
}

System.out.println("The total number of strings which contain key phrase 1 is " + keyPhrase1);
System.out.println("The total number of strings which contain key phrase 2 is " + keyPhrase2);
System.out.println("The total number of strings which contain key phrase 3 is " + keyPhrase3);

avgLower = numLowerTot / userInput.length();
avgUpper = numUpperTot / userInput.length();

System.out.println("The average number of uppercase letters per string was: " + avgUpper);
System.out.println("The average number of lowercase letters per string was: " + avgLower);





}



}

coze
11-14-2006, 12:26 AM
first of all, String is not a good name for a custom class. If I were you, I would choose another name to prevent conflicts withe java String class.
Did you check what indexOf returns when it can not find the string ? And actually did you check if indexOf searches for string occurances ? I hav ea feeling it works only for characters.

second, you probably have a problem trying to count the characters inside the string. if you want to count the characters inside userInput, you should check userInput[j].isLowerCase() or something like that. c1 and c2 are not needed.

it's been a long time I didn't mess with java, so I can't remember all the bits. But these are my first impressions, hope it helps.