Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2008, 07:40 PM
Member
 
Join Date: Dec 2007
Posts: 17
Jman is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-05-2008, 08:01 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-05-2008, 08:41 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
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.

Code:
// 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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-06-2008, 12:22 AM
Member
 
Join Date: Jul 2008
Posts: 26
Jeremy is on a distinguished road
Quote:
Originally Posted by Nicholas Jordan View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-06-2008, 12:25 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-07-2008, 09:22 PM
Member
 
Join Date: Dec 2007
Posts: 17
Jman is on a distinguished road
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 ++;


}
}

}
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-08-2008, 12:29 AM
Member
 
Join Date: Jul 2008
Posts: 26
Jeremy is on a distinguished road
I stand corrected on 97 being b.

Quote:
Originally Posted by Jman View Post
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?

Code:
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.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-08-2008, 12:49 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
Norm is on a distinguished road
Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-08-2008, 03:45 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
char++
Quote:
Originally Posted by Jeremy View Post
To expand on that, (...snip...)
Code:
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-08-2008, 03:49 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
auto-conversion
Quote:
Originally Posted by Norm View Post
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-08-2008, 05:45 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
Norm is on a distinguished road
I hope the OP isn't throughly confused by now.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-18-2008, 10:41 PM
Member
 
Join Date: Dec 2007
Posts: 17
Jman is on a distinguished road
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;
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-18-2008, 10:59 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
use cast
Quote:
Originally Posted by Jman View Post
Sorry for the late reply I was away.
Me too, working on sluggish machine.
Quote:
Originally Posted by Jman View Post
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
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-18-2008, 11:05 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Quick exploring
Hello JMan

If you run this code and ignore the PC speaker going nuts:
Code:
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:
Code:
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
Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-20-2008, 05:00 PM
Member
 
Join Date: Dec 2007
Posts: 17
Jman is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-20-2008, 08:34 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Hello Jman
Quote:
Originally Posted by Jman View Post
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:
Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 07-20-2008, 09:13 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
Norm is on a distinguished road
Why do you use the decimal number 97 when the char 'a' would be much clearer?
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 07-21-2008, 03:34 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
loss of precision?
Quote:
Originally Posted by Jman View Post
....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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
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


All times are GMT +3. The time now is 10:06 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org