|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-05-2008, 07:40 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 17
|
|
|
need help with arrays
Hi All
I need some help please I am trying to create an array that holds all the letters of the alphabet and then prints them out. this is my code:
public class Alphabet {
public static void main (String [] args)
{
char[]myLetters = new char[26];
for (char index = 0; index < myLetters.length; index++)
{
myLetters[index] = 97 ;
}
for (char index = 0; index < myLetters.length; index++)
{
System.out.println(myLetters[index]);
}
}
}
my output:
is just a line of 26 b's.
Where am I giong wrong?
|
|

07-05-2008, 08:01 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 421
|
|
|
you have to char++ somewhere
__________________
Please provide your feedback on our To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-05-2008, 08:41 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 421
|
|
|
simple ascii printable character set.
Normally, it is better not to do this but the handy utility here is a good tool for beginners to have: It shows the base ascii printable characters.
// An array may not hold all the letters of the alphabet.
// A print all ascii char utility. Save some beginners
// several sluggish starts, use a StringBuffer
public class AlphaZetaBet
{
// String Buffer - we do not know how far it will go.
StringBuffer stringBuffer = new StringBuffer(26);
String getCharacters()
{
// note the single tick marks for a char
char c = ' ';// space, the beginning of printable
// This stops on backspace character.
do
{
stringBuffer.append(c++);
}
while(c < 0x7f);
return stringBuffer.toString();
}
public static void main (String [] args)
{
AlphaZetaBet azb = new AlphaZetaBet();
System.out.println(azb.getCharacters());
}
}
Bye all!
__________________
Please provide your feedback on our To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-06-2008, 12:22 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 26
|
|
Originally Posted by Nicholas Jordan
you have to char++ somewhere
To expand on that, your for loop is assigning the number 97 to every slot in your array. As you can see, number 97 is the ASCII numerical value for b.
Last edited by Jeremy : 07-06-2008 at 12:27 AM.
|
|

07-06-2008, 12:25 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
|
|
|
myLetters[index] = 97 ;
Why are you assigning 97 to each element in the array?
Are you sure that b's are output and not a's?
If you look at the binary value for the character b as decimal, it is 98, as hex 0x62
'a's value in decimal is 97 or in hex is 0x61
Why are you using the char type for an index in your for loops?
Normally int is used.
To put all the characters the alphabet in an array using a loop like you are doing requires that the values of the letters be contiquous. Then you must know the value of the first/smallest.
The ascii letter values are not contiguous. They come in two ranges, one for lower case: a-z and one for upper case: A-Z. So you'd need two loops to put both lower and upper case letters.
Since char can treated as an integer, ie you can do arithmetic on it, then the assignment statement in your loop that assigns a value to each element would be:
array[index] = charValue++; // put in a char and incr value to next char
charValue would start at 'a' for lower case and 'A' for upper case.
Last edited by Norm : 07-06-2008 at 12:35 AM.
|
|

07-07-2008, 09:22 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 17
|
|
|
Hi everyone who responded to my call for help.
Norm: The value 97 was the ASCii for a but because I had +1 it turned the value to 98 which is b.
Jeremy you said that I have to you have to char++ somewhere. I tiried everywhere. where do I use char++.
changed the code just a bit.
public class Alphabet {
public static void main (String [] args)
{
char[]myLetters = new char[26];
for (int index = 0; index < myLetters.length; index++)
{
myLetters[index] = 'a';
}
for (int index = 0; index < myLetters.length; index++)
{
System.out.println(myLetters[index]);
index ++;
}
}
}
|
|

07-08-2008, 12:29 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 26
|
|
I stand corrected on 97 being b.
Originally Posted by Jman
Jeremy you said that I have to you have to char++ somewhere. I tiried everywhere. where do I use char++.
I didn't. Norm did.
But, do you see what you're doing wrong here?
for (int index = 0; index < myLetters.length; index++) {
myLetters[index] = 'a';
}
You are assigning the letter 'a' to every spot in the array. You need to increment that value somehow.
for (int index = 0; index < myLetters.length; index++) {
myLetters[index] = 97 + index;
}
Explain to me what this loop is doing so I know you understand it. 
|
|

07-08-2008, 12:49 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
|
|
for (int index = 0; index < myLetters.length; index++)
{
System.out.println(myLetters[index]);
index ++;
}
What happens to the value of index as you go thru this loop?
it starts at 0, then is incremented by 1 at the bottom of the loop to 1 and then it is incremented by 1 to 2 in the for()
I would expect this loop to output all the even positioned elements in the array.
|
|

07-08-2008, 03:45 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 421
|
|
|
char++
Originally Posted by Jeremy
To expand on that, (...snip...)
stringBuffer.append(c++);
That should increment the character, Norm is an experienced worker at computer coding and knows how loops must be approached in a tediously detailed manner. I am swamped right now. If this does not do what I think it does then let Norm know, I will assist Norm.
He is our guide. { yuk-yuk }
__________________
Please provide your feedback on our To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-08-2008, 03:49 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 421
|
|
|
auto-conversion
Originally Posted by Norm
Why are you using the char type for an index in your for loops?
Normally int is used.
( ... and related matters .... )
Norm, Java will often do an auto-conversion beyond the reach of the coder. It used to be that Java would back propogate the conversion in such a manner that compiler errors would show up somewhere else, leaving the beginner totally dumbfounded.
I do not know if that happens today, or in this code.
__________________
Please provide your feedback on our To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-08-2008, 05:45 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
|
|
|
I hope the OP isn't throughly confused by now.
|
|

07-18-2008, 10:41 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 17
|
|
|
Hi Jeremy
Sorry for the late reply I was away.
With regards to your question:
Code:
for (int index = 0; index < myLetters.length; index++) {
myLetters[index] = 97 + index;
}
the above code states that for the first location in the array should be a b and every one after that could be +1 so cdefghij etc.
I tired this code but it gives me a error message stating the following:
possible loss of precision on line myLetters[index] = 97 + index;
|
|

07-18-2008, 10:59 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 421
|
|
|
use cast
Originally Posted by Jman
Sorry for the late reply I was away.
Me too, working on sluggish machine.
Originally Posted by Jman
possible loss of precision ...
Usually that is fixed with a cast:
array[index] = (char) val;
__________________
Please provide your feedback on our To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-18-2008, 11:05 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
Quick exploring
Hello JMan
If you run this code and ignore the PC speaker going nuts:
public class Main{
public static void main(String[] arg) {
for (int i = 0; i < 256; i++){
System.out.println(Integer.toString(i) + "\t=\t" + (char) i);
}
}
}
You will see a list of integers with their corresponding characters. Notice that integers where used since the Java byte goes from -128 to 127. Selecting the integers from 65 to 90 will give the alphabet. So the code will then be:
public class Main{
public static void main(String[] arg) {
char[] alphabet = new char[26];
for (int i = 65, counter = 0; i <= 90; i++){
alphabet[counter] = (char) i;
counter++;
}
for (char letter : alphabet){
System.out.print(letter + " ");
}
System.out.println();
}
}
Which gives output
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
I hope that helps you JMan 
__________________
If your ship has not come in yet then build a lighthouse.
Last edited by tim : 07-18-2008 at 11:08 PM.
|
|

07-20-2008, 05:00 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 17
|
|
|
Hi Nicholas and Tim
first thank you both very much for all the help. Nicholas you said that array[index] = (char) val; if I used what you showed me here it could fix a possible loss of precision. I tried to use the logic in every what way and I gave in at the end.
But thank you Tim you code does work so I want to see what is wrong with my code compared to yours.
|
|

07-20-2008, 08:34 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
Hello Jman
Originally Posted by Jman
Hi Nicholas and Tim
first thank you both very much for all the help. Nicholas you said that array[index] = (char) val; if I used what you showed me here it could fix a possible loss of precision. I tried to use the logic in every what way and I gave in at the end.
But thank you Tim you code does work so I want to see what is wrong with my code compared to yours.
Here is your code, except changed the blue part:
public class Main{
public static void main (String [] args) {
char[] myLetters = new char[26];
for (char index = 0; index < myLetters.length; index++) {
myLetters[index] = (char) (97 + index);
}
for (char index = 0; index < myLetters.length; index++) {
System.out.println(myLetters[index]);
}
}
}
Yours just print lower case alphabet letters, but does essentially the same thing. I think your method is simpler though. I just wanted to rise to the challenge. It appears that variable index can be of type char, byte or int. Java does the converting for us. The problem was, you must cast to explicitly convert between certain primitive types. Actually, the privative types are "boxed" into their Object equivalents, casted, and then converted back to the correct primitives. This is called auto-boxing. And it makes our lives a lot easier.
I hope this clears everything up JMan. 
__________________
If your ship has not come in yet then build a lighthouse.
|
|

07-20-2008, 09:13 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
|
|
|
Why do you use the decimal number 97 when the char 'a' would be much clearer?
|
|

07-21-2008, 03:34 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 421
|
|
|
loss of precision?
Originally Posted by Jman
....possible loss of precision.....
Tim shows the correct code, at least that's the way I would do it and skip the auto-boxing.
What loss of precsions means is thus:
110111 100001 <- One datatype.
11010000011000111100 <- Another datatype
The big one won't fit in the little one. Compiler warns you if you do it. Placing (char) at the point shown tells compiler you are aware of the loss.
__________________
Please provide your feedback on our To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Arrays |
bunbun |
New To Java |
1 |
04-09-2008 03:24 AM |
| new to arrays |
jimJohnson |
New To Java |
1 |
04-08-2008 03:45 PM |
| 2D-Arrays |
kbyrne |
New To Java |
1 |
02-07-2008 11:08 PM |
| arrays help |
Warren |
New To Java |
6 |
11-23-2007 08:23 PM |
| Problems with arrays |
Marcus |
New To Java |
2 |
07-04-2007 09:10 AM |
|
|