Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-25-2007, 01:05 AM
zee zee is offline
Newbie
 
Join Date: Feb 2007
Posts: 2
Rep Power: 0
zee is on a distinguished road
Default Next Line and line lengths

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,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 02-25-2007, 01:29 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

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:
Code:
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-25-2007, 02:00 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-25-2007, 02:31 AM
zee zee is offline
Newbie
 
Join Date: Feb 2007
Posts: 2
Rep Power: 0
zee is on a distinguished road
Default

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 11:53 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads