|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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, |
| Sponsored Links |
|
|
|
|||||
|
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
Code:
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();
}
}
Last edited by John; 02-25-2007 at 02:04 AM. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VB6.0:Tutorial, Command Line | TcM | VB Tutorials | 9 | 08-25-2008 01:19 PM |
| Import SQL file command line | Ronin | Database & Database Programming | 14 | 01-25-2008 03:56 PM |
| HTML Basic Formatting | clookid | Tutorials | 14 | 03-06-2007 04:10 PM |
| PHP command Line | dirkfirst | PHP Forum | 1 | 10-26-2006 11:31 AM |
| Line breaks not working | dirkfirst | PHP Forum | 4 | 06-16-2006 06:20 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |