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