Im abit confused about the problem im solving right now.
it says that i will create a program that would let me input any word, and count how many letters are there.
and afterwards i will also make another statement letting me input any letter,then the program would determine if the letter i entered is part of the word i entered.

im only until the input of the word and the letter count.
here's my unfinished code

import java.util.*;

class array
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String a;
int x;
boolean[]word=new boolean[52];
System.out.println("Please enter a word: ");
a=input.next();
System.out.println("There word  "+a+" has "+a.length()+" letters");
for(x=0;x<a.length();x++)
{
word[x]=true;
}
}
}

heres the expected complete output

Please enter a word: notebook
The word notebook has 8 letters
Please enter any letter: k
Letter k is part of the word notebook


i need help :( i really know nothing about boolean arrays.

Recommended Answers

All 6 Replies

... i really know nothing about boolean arrays.

Thanks for editing your original post :) Having the CODE tags makes it easier to read and follow. Nobody will code your homework for you, but you will get hints!

Here is your hint:

26 letters in the alphabet
52 possibilities (Upper and Lower case)

Your array is built for recording that information.

You don't need a boolean array. You need a String. Then you need to iterate over the String, determining if each piece of the String is a character or not. Check out the isLetter method of the Character class. You can determine if a specific piece (i.e. index) of a String is a letter by using the String class's charAt method, then getting that character and using the Character class's isLetter method.

Oh, and for the later part that you mentioned where you need to enter a letter, you can do that as follows:
1. Use the String class's toCharArray method on your String.
2. For whatever character they enter, loop through the array, seeing if the character they entered == the one in the array.

oh and another thing. the program would count how many letters did i guessed from the word and the program will end when i enter a digit or a symbol.


here's my new code

import java.util.*;

class array
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String a,b,d;
char c;
int x;
char[]word=new char[52];
System.out.println("Please enter a word: ");
a=input.next();

System.out.println("The word  "+a+" has "+a.length()+" letters");
for(x=0;x<a.length();x++)
{
word[x]=a.charAt(x);
}


do
{
System.out.println("Enter any letter: ");
b=input.next();
d=b.toUpperCase();
c=b.charAt(0);
int sum=0,total=0;

if(Character.isLetter(b.charAt(0)))
{
	for(x=0;x<a.length();x++)
	{
	if(c==word[x])
	sum++;
	}

	System.out.println("There are "+sum+" "+d+"'s");
	total=total+sum;
	}

else
	{
	System.out.println("You have guessed a total of "+total+" letters");
	}

}
while(Character.isLetter(b.charAt(0)));


}
}

i dont know what to do for the program to be able to count on how
many is the total guessed letters.

the output should look like this...

Please enter a word: hello
The word hello has 5 letters
Enter any letter: e
There are 1 E's
Enter any letter: l
There are 2 L's
Enter any letter: 5
You have guessed a total of 3 letters <- i am having difficulties on this part.

In your do while loop just use a counter. So right before you enter the do while loop, declare and initialize a counter with

int counter = 0;

then inside the loop, right before you say "enter any letter", do

counter++;

Then right after the loop ends, do

System.out.println("You guessed " + counter + " times");

dude thanks a lot now here's my perfect code...

import java.util.*;

class array
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String a,b,d;
char c;
int x,total=0;
char[]word=new char[52];
System.out.println("Please enter a word: ");
a=input.next();

System.out.println("The word  "+a+" has "+a.length()+" letters");
for(x=0;x<a.length();x++)
{
word[x]=a.charAt(x);
}


do
{
System.out.println("Enter any letter: ");
b=input.next();
d=b.toUpperCase();
c=b.charAt(0);
int sum=0;

if(Character.isLetter(b.charAt(0)))
{
	for(x=0;x<a.length();x++)
	{
	if(c==word[x])
	sum++;
	}

	System.out.println("There are "+sum+" "+d+"'s");
	total=total+sum;
	}

else
	{
	System.out.println("You have guessed a total of "+total+" letters");
	}

}
while(Character.isLetter(b.charAt(0)));


}
}

thanks a lot for the tips.

No problem. I did notice what some might call a mistake in your code, though. If your word is longer than 52 characters, (which is highly unlikely, but bear with me), your output would be incorrect. You could fix this by doing something like

char[] array;
//setup scanner code. . 
String word = scanner.next();
array = word.toCharArray();

Or you could also do

char[] array;
//setup scanner code. . 
String word = scanner.next();
array = new array[word.length()];

The second example I posted would allow you to change barely any of your code, but make it that so (for example) if your professor was being particularly nitpicky, he couldn't take points off.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.