Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Loops-Counters

loop

  • Please log in to reply
4 replies to this topic

#1 sthenri

sthenri

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 21 October 2007 - 05:28 AM

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.
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();
}
}
}

  • 0

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 21 October 2007 - 08:47 AM

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?
  • 0

#3 sthenri

sthenri

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 21 October 2007 - 08:55 AM

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.
  • 0

#4 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 21 October 2007 - 09:52 AM

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


//strings from the user
String str= "The quick brown fox jumped over the lazy dog.";
String input2 = "h";

//convert input2 to a char
char chr = input2.toCharArray()[0];

//number of times the char was present
int num = 0;

for(int i=0; i<str.length(); i++) {
if(str.toCharArray()[i] == chr) {
num++
}
}

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.
  • 0

#5 sthenri

sthenri

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 22 October 2007 - 12:52 PM

Thank You,
I will study the code and post again.
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download