Help with straing arrays
Example, if i have this string array:
int count=0;
for (int i = 0; i < n; i++)
{
String [] alpha={"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"};
System.out.println(count" " + alpha[ i] );
count++;
}
eventually the loop reach to array [25] it will stop because array will out of bound if it go above 26. the question is how can i make the array go back to the begining once it reach to the end ?
Message was edited by:
bail_w
Message was edited by:
bail_w
[597 byte] By [
bail_wa] at [2007-11-27 9:50:18]

1) First of all, why is your String array alpha inside the loop? Is alpha changing with every iteration? Nope. So declare it before your loop. That way you can use the size of the array as an upper bound of any for-loop which helps to prevent array out of bounds errors.
2) You say that want to loop through the array again but you don't say how many times you want this to occur.
3) Consider simplifying your array to an array of char like so:
String alphaBet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alpha = alphaBet.toCharArray();
4) To loop through your array x times, use a second for loop outside the first one.
public class Fubar2
{
private void loopThruAlphaBet(int j)
{
........
........
........
}
public static void main(String[] args)
{
Fubar2 foo = new Fubar2();
for (int j = 0; j < 4; j++)
{
foo.loopThruAlphaBet(j);
}
}
}
Note that I did one loop in one method and the second outside loop in another method that called the first one. You could have all of this in one method if you desire. I passed an int parameter "j" (which is just the outer loop index) to the inner loop method in case you wanted to display the total count which is easy to calc if you know the indices of both loops.
You could do it with a single loop. Have an if statement inside the loop. When the loop counter gets to max set it back to zero. This of course will give you an infinite loop unless you add some other condition to exit.
> 1) First of all, why is your String array alpha
> inside the loop? Is alpha changing with every
> iteration? Nope. So declare it before your loop.
> That way you can use the size of the array as an
> upper bound of any for-loop which helps to prevent
> array out of bounds errors.
This is what I meant by using the length of the array to determine how to loop through it:
String[] myArray = {"what", "the", "fuck"};
for (int i = 0; i < myArray.length; i++)
{
System.out.print(myArray[i] + " ");
}
System.out.println();
the key is the statement i < myArray.length; above. If the array size ever changes, my for loop will automatically change with it.It's almost idiot-proof.
loop it through as many times as i want. also what is this?private void loopThruAlphaBet(int j){........................}
> what is this?Did you read pete's reply? He said that he used two methods. One called the other x number of times. In the other method you would put your code that you have already written to print out the alphabet.
> loop it through as many times as i want. also what is
> this?
>
> private void loopThruAlphaBet(int j)
>{
>........
>........
>........
> }
I was providing a nucleus for you to build off of. You don't want me or anyone else to do all of your work, otherwise you won't learn. The "...." was for you to fill in.
> Did you read pete's reply? He said that he used two
> methods. One called the other x number of times. In
> the other method you would put your code that you
> have already written to print out the alphabet.
But you don't have to do it the way I did it, of course. In fact, it is probably better if you don't, if instead you do it your way,... as long as it works correctly. The way for you to learn is to do as much of your own coding as possible.
Good luck.
class LottoMachine
{
public static void main (String[] args)
{
int n = Integer.valueOf(args[0]).intValue();
int counter =0;
Lotto myNumber = new Lotto(5);
String alphaBet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char [] alpha = alphaBet.toCharArray();
for (int i = 0; i <n; i++)
{
if(counter==10)
{
System.out.println();
counter=0;
}
System.out.print(" " + alpha[i]);
myNumber.printIt();
System.out.println();
counter ++;
}
System.out.println();
}
}
This is what i was doing, printing out A-Z with numbers generate such as :
A 45 64 54 54 05
B 54 45 45 54 87
.
.
.
Z 12 54 78 34 31
but after the first loop till 26, it will give me a exception error which i don't want. and my question is how can i make it continue going from A-Z again.>
> my question is how can i make it continue going from A-Z again.>and we have told you how to do it.
> > my question is how can i make it continue going> from A-Z again.>> > and we have told you how to do it.if i understood, i wouldnt post my question again.
class Test {
public static void main(String[] argv) {
int offset = (int)'A';
for( int j = 0; j < 10; j++ ) {
for( int i = offset; i < (offset + 26); i++ ) System.out.print((char)i);
System.out.println();
}
}
}
> > > my question is how can i make it continue going
> > from A-Z again.>
> >
> > and we have told you how to do it.
>
> if i understood, i wouldnt post my question again.
Don't be in such a hurry here. You must work this out on your own. Think on this, play with your code. Again, this is your assignment, and only you will benefit if you can figure this out. Again, good luck.
> class Test {
> public static void main(String[] argv) {
> int offset = (int)'A';
> for( int j = 0; j < 10; j++ ) {
> for( int i = offset; i < (offset + 26); i++ )
> + ) System.out.print((char)i);
> System.out.println();
> }
> }
> }
To Navy_Coder:
If we're going to be concise, why not: for (char c = 'A'; c <= 'Z'; c++) System.out.println(c);?
To the original poster, don't do this stuff above!
> To Navy_Coder:
> If we're going to be concise, why not: for (char c =
> 'A'; c <= 'Z'; c++) System.out.println(c);?
>
Because I suck and didn't come up with that one on my own.
> To the original poster, don't do this stuff above!
Why not? It's perfectly legal. (Though, if you actually turn that in as part of a homework assignment - you'll likely fail...)
i been trying to figure this out for the past day and i couldn't get it work. anyway, thanks for your help, i am just going to turn it in. forget it.
> i been trying to figure this out for the past day and> i couldn't get it work. anyway, thanks for your help,> i am just going to turn it in. forget it.Now, there's a winning attitude if I ever saw one.
oh boo hoo!
get user to enter how many number sets they want
declare some counter to indicate which letter to print
loop set number of times {
print letter at counter
print numbers
print new line
increment counter
if counter has reached max {
set counter back to zero
}
}
Can you help Me! to have this code:
the given problem is:
Write a java program that counts existence of each alphabet letter from "A - Z"? It should be input from a user. It should be an array.
for example the user will input the words: " thank you "
the output will:
a=1
h=1
k=1
n=1
o=1
t=1
u=1
y=1
Please help me to have this code. thank you!!!!!!!!
Hi julbsit,
You'd be better off on a new post. This isn't the same question as the other one.
How much code do you have so far? People are willing to help with specific problems. If you can't figure out where to begin, then explain what algorithm you are thinking about using, and people can help from there.
Hint: most of the above replies seem unwilling to write out someone else's homework. After reading those, I can't figure out why your first statement is "Please help me to have this code". It will probably give the impression that you don't want to do any of the work yourself.