CDROM-Guide forums  

PDA

View Full Version : JAVA help


   
aznkidlee
Mar 03, 2004, 07:15 PM
Does anybody see anything wrong with this code?


public class ACSLMAN
{
public static void Keyboard (String input)
{

Keyboard kb = new Keyboard ();

System.out.print (">");
input = kb.readString();
System.out.println(input);

}

public static void check (String input)
{

// char [] letters = new char [25];
// int [] counter = new int [25];
int countA=0;
int countB=0;
int index=0;

while (index < input.length())
{
if (input.charAt(index) == 'a')
{
countA = countA + 1;
}

if (input.charAt(index) == 'b')
{
countB = countB + 1;
}
index=index +1;
}
System.out.println (countA);
System.out.println (countB);
}


public static void main ()
{
String input ="";
int counter = 0;

while (counter < 2)
{
Keyboard(input);
check(input);
counter++;
}
}

}


The program is not done yet, but basically I want this program to count up the number of letters of the users input.

The output of this program is incorrect. I enter aa and it gives me 0. It should be 2. I am not sure whats wrong with this so I am wondering if anybody can help me?

zachariah
Mar 03, 2004, 09:17 PM
Originally posted by aznkidlee
public static void main ()
{
String input ="";
int counter = 0;

while (counter < 2)
{
Keyboard(input);
check(input);
counter++;
}

I think this is where your problem is when you call Keyboard and check methods you are providing them with a blank string, what you should be doing is having your keyboard method return your string and then pass it to your method but I wouldn't use the main method as this requires arguments to be passed via the command line. You will be able to see what is going on better if you dont use static methods but using static methods is bad practise anyway, it violates all object oriented concepts and causes security nightmares