Closed Thread
Results 1 to 5 of 5

Thread: Loops-Counters

  1. #1
    sthenri is offline Newbie
    Join Date
    Oct 2007
    Posts
    15
    Rep Power
    0

    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 06:32 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    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?

  4. #3
    sthenri is offline Newbie
    Join Date
    Oct 2007
    Posts
    15
    Rep Power
    0

    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.

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    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

    [highlight="Java"]
    //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);
    [/highlight]

    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.

  6. #5
    sthenri is offline Newbie
    Join Date
    Oct 2007
    Posts
    15
    Rep Power
    0

    Hi

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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Loops in C#
    By Bobthecoder in forum C# Programming
    Replies: 3
    Last Post: 10-26-2011, 08:56 PM
  2. Mysql counters fields vs realtime track
    By webcodez in forum PHP Development
    Replies: 5
    Last Post: 04-19-2010, 04:10 PM
  3. Question about arrays/counters
    By Sparty2009 in forum C and C++
    Replies: 1
    Last Post: 11-12-2009, 07:59 PM
  4. Loops
    By Sionofdarkness in forum C and C++
    Replies: 7
    Last Post: 07-28-2006, 05:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts