Reputation: 11
When I run the program it PRINTS out the numbers in the indexes, instead of printing out the word. The idea of the program is to create random sentences, Anyone know what I'm doing wrong here?
public class TestShoutBox5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String Subjects[] = new String[]{"My Teacher","I","You"
};
Random RandomSubject=new Random();
int S = RandomSubject.nextInt(4);
String Objects[] = new String[]{"Homework", "Work","Quiz"
};
Random Randomobjects=new Random();
int O = Randomobjects.nextInt(4);
String Verbs[] = new String[]{"Studying", "Playing","Working"
};
Random Randomverbs=new Random();
int R = Randomverbs.nextInt(4);
String Adjectives[] = new String[]{"Smart", "Tall","Hard"
};
Random RandomAdjectives=new Random();
int A = RandomAdjectives.nextInt(4);
String Adverbs[] = new String[]{"quickly", "everywhere","fast"
};
Random RandomAdverb=new Random();
int Ad = RandomAdverb.nextInt(4);
System.out.print("Your Sentence"+S+O+R+A+Ad);
}
}
Upvotes: 0
Views: 128
Reputation: 7255
You are printing the keys,you want the content of the array in that cell so:
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Also you can do it with 1 Random:
public class TestShoutBox5 {
/**
* @param args
* the command line arguments
*
*/
public static void main(String[] args) {
// TODO code application logic here
String Subjects[] = new String[] { "My Teacher", "I", "You" };
Random random = new Random(); // One Random
int S = random.nextInt(3);
String Objects[] = new String[] { "Homework", "Work", "Quiz" };
int O = random.nextInt(3);
String Verbs[] = new String[] { "Studying", "Playing", "Working" };
int R = random.nextInt(3);
String Adjectives[] = new String[] { "Smart", "Tall", "Hard" };
int A = random.nextInt(3);
String Adverbs[] = new String[] { "quickly", "everywhere", "fast" };
int Ad = random.nextInt(3);
System.out.print("Your Sentence" + " " + Subjects[S] + " " + Objects[O] + " " + Verbs[R] + " " + Adjectives[A]
+ " " + Adverbs[Ad]);
}
}
Upvotes: 2
Reputation: 742
The problem is that you are printing out the indices instead of the words at hose indices in the arrays. Instead of:
System.out.print("Your Sentence"+S+O+R+A+Ad);
Do this:
System.out.print("Your Sentence"+ Subjects[S]+Objects[O]+Verbs[R]+Adjectives[A]+Adverbs[Ad]);
Edit (from @kyriacos_k's comment):
As the array indices go from 0 to the length of the array - 1, the Random object should be creating values from 0 - 2 instead of 0 - 3. To solve this, use .nextInt(3)
instead of .nextInt(4)
Upvotes: 2