Connect with Facebook 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 10-21-2007, 08:28 AM
Newbie
 
Join Date: Oct 2007
Posts: 15
Rep Power: 0
sthenri is an unknown quantity at this point
Default Loops-Counters

Hi, I started learning Java about 9 months ago and here is where it gets tricky for me. When I am learning loops and arrays.

I do not need to buy more books or look at more tutorials because I have looked at hundreds of books, tutorials, and programs and at one point I had what I needed and then I didn't save-somewhere in the middle of the night last week. Many forums will not help, only suggest different tutorials or books, I am close and I do want tips that push me in the right direction. One book I recommend is Big Java.

Here is a program that does some of what I would like it to do, it does not allow the user to input the sentence into an array and then input a character to search for that value. It's fairly simple to find a value when it's a number, I can write a simple program so far. But I feel I am missing many crucial elements. I want the program to allow the user input a sentence or several values (with a dialog box), then input the value to find in that sentence or group of numbers, then the program totals the number of times the value is found.
Code:
import java.io.*;

public class WordCount{
private static void linecount(String fName, BufferedReader in) throws IOException{
long numChar = 0;
long numLine=0;
long numWords = 0;
String line;
do{
line = in.readLine();
if (line != null){
numChar += line.length();
numWords += wordcount(line);
numLine++;
}
}while(line != null);
System.out.println("Enter Sentence: " + fName);
System.out.println("Enter Character: " + fName);
System.out.println("Number of characters in sentence: " + numChar);
System.out.println("Number of words: " + numWords);
System.out.println("Number of Lines: " + numLine);
}
private static void linecount(String fileName){
BufferedReader in = null;
try{
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
linecount(fileName,in);
}
catch(IOException e){
e.printStackTrace();
}
}
private static long wordcount(String line){
long numWords = 0;
int index = 0;
boolean prevWhiteSpace = true;
while(index < line.length()){
char c = line.charAt(index++);
boolean currWhiteSpace = Character.isWhitespace(c);
if(prevWhiteSpace && !currWhiteSpace){
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
return numWords;
}
public static void main(String[] args){
long numChar = 0;
long numLine=0;
String line;
try{
if (args.length == 0)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
line = in.readLine();
numChar = line.length();
if (numChar != 0){
numLine=1;
}
System.out.println("Number of characters in sentence: " + numChar);
System.out.println("Number of words: " + wordcount(line));
System.out.println("Number of lines: " + numLine);
}else{
for(int i = 0; i < args.length; i++){
linecount(args[i]);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}

Last edited by sthenri; 10-21-2007 at 08:32 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-21-2007, 11:47 AM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,306
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default

So you want to enter something like:

Enter a sentence:
=> "The quick brown fox jumped over the lazy dog"

Then,
Enter a character:
=> "q"

And what is it suppose to return? The placement in the sentence? Do spaces count? Would q be 4 or 5? What if there are more than one instance of the character in the sentence?

Have I completely missed the point?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-21-2007, 11:55 AM
Newbie
 
Join Date: Oct 2007
Posts: 15
Rep Power: 0
sthenri is an unknown quantity at this point
Default Yes

Hi Sidewinder, thanks and no you didn't miss the point. Yes the user is prompted to enter a sentence, then prompted to enter a character within the sentence, then the program outputs a number, which is the number of times it finds that character.

The program displays the number of times the character appears in the sentence. The program is searching the array for the character entered, and returns the number of times it's found.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-21-2007, 12:52 PM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,306
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default

Well you would store the inputed sentence as a string. Then you could call the toCharArray() method on the string, which would convert the string into an array of characters so you can reference them by their index.

Lets assume the inputed string is called str and the inputed character is called input2. The first thing you have to 'note' is that input2 is a string, so you will have to convert that into a char.

Once you have converted the input2 string to a char, lets call that variable chr. You can then create a loop from 0 to str.length() and check if str.toCharArray()[i] is the same as chr. If it is, increment a counter and start the loop again

Java Code:
  1. //strings from the user
  2. String str= "The quick brown fox jumped over the lazy dog.";
  3. String input2 = "h";
  4.  
  5. //convert input2 to a char
  6. char chr = input2.toCharArray()[0];
  7.  
  8. //number of times the char was present
  9. int num = 0;
  10.  
  11. for(int i=0; i<str.length(); i++) {
  12.     if(str.toCharArray()[i] == chr) {
  13.         num++
  14.     }
  15. }
  16.  
  17. System.out.println(num);

One thing this doesn't account for is the fact that 'H' and 'h' are different. So if you run this using 'h' it will return 1, but using 'H' it will return 0. Of course you can account for using other methods, but this will get you started.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-22-2007, 03:52 PM
Newbie
 
Join Date: Oct 2007
Posts: 15
Rep Power: 0
sthenri is an unknown quantity at this point
Default Hi

Thank You,
I will study the code and post again.
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Loops reachpradeep Database & Database Programming 0 03-04-2007 09:12 AM
JavaScript:Tutorial, Loops TcM Javascript 4 12-13-2006 08:53 AM
JavaScript:Tutorial, Break Out Of Loops TcM Javascript 0 12-08-2006 05:35 AM
Loops Sionofdarkness C and C++ 7 07-28-2006 07:53 PM


All times are GMT -5. The time now is 09:55 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads