I just have a few questions that I can't seem to find the answers to, as I'm trying to write a program for homework. If anyone could answer these questions (listed in order of importance), I would GREATLY appreciate it.
1. How do I tell my program to read one line, and then do something else, and then read the next line?
2. How do I check the number of lines I have in a file?
3. With an array, how can I tell how many characters are in that array?
4. When I'm given an array of strings, how can I construct another 2 dimensional array that lists how frequently each letter appears?
Thanks,
Next Line and line lengths
Started by
Guest_zee_*
, Feb 24 2007 10:05 PM
3 replies to this topic
#1
Guest_zee_*
Posted 24 February 2007 - 10:05 PM
Guest_zee_*
|
|
|
#2
Posted 24 February 2007 - 10:29 PM
1) Read a line in the console or from a file?
2) No idea
3) Create a loop for the length of the array, the get the length of each element in the array and add it to the previous value, maybe something like this:
4) Pretty much just another loop that checks every letter of every word in every element in the array and adds the values to the 2D array, that should get you started but if you need more help let me know....but i dont think there is a native function to do this
2) No idea
3) Create a loop for the length of the array, the get the length of each element in the array and add it to the previous value, maybe something like this:
int size = 0;
for(int i = 0; i < myArray.length(); i++){
size = myArray[i].length() + size;
}
4) Pretty much just another loop that checks every letter of every word in every element in the array and adds the values to the 2D array, that should get you started but if you need more help let me know....but i dont think there is a native function to do this
#3
Posted 24 February 2007 - 11:00 PM
For 4, im not sure why you would want to use a two dimensional array, but if I understood the functionality correctly this should do 3 and 4
package general;
public class ArrayFunctions {
private String[] myArray = new String[] {"john", "steve", "jordan", "lauren", "tom", "bobby", "bill", "andrew"};
private String[] letters = new String[] {"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"};
private int totalLength = 0;
private Integer[] ofEachChar = new Integer[26];
public ArrayFunctions(){
TotalLength();
AmountOfEachChar();
}
public void TotalLength(){
for(int i = 0; i < myArray.length; i++){
totalLength = myArray[i].length() + totalLength;
}
System.out.println("The character length of the array is " + totalLength);
}
public void AmountOfEachChar(){
//check each element
for(int i = 0; i < myArray.length; i++){
//check each word
for(int p = 0; p < letters.length; p++){
//Set each element in "ofEachChar" to 0 to avoide a NullPointerException
if(ofEachChar[p] == null){
ofEachChar[p] = 0;
}
//and every letter of each word
for(int j = 0; j < myArray[i].length(); j++){
//if the j'th character is the same as the p'th letter in the alphabet
if(String.valueOf(myArray[i].charAt(j)).toUpperCase().equals(letters[p])){
//add 1 to the p'th ofEachChar element
ofEachChar[p] = 1 + ofEachChar[p];
}
}
}
}
//Print the results
for(int i = 0; i < letters.length; i++){
System.out.println("There are " + ofEachChar[i] + " " + letters[i] + "'s");
}
}
public static void main(String[] args){
new ArrayFunctions();
}
}
#4
Guest_zee_*
Posted 24 February 2007 - 11:31 PM
Guest_zee_*
Thanks for the reply, but #1 is reading it from a text file. I can't seem to find a next line function on buffered reader, and I can't find a length function for Scanner... help would be great... thanks!


Sign In
Create Account

Back to top









