+ Reply to Thread
Results 1 to 5 of 5

Thread: Loops-Counters

  1. #1
    Newbie sthenri is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    15

    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.

  2. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25
    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?

  3. #3
    Newbie sthenri is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    15

    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.

  4. #4
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25
    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.

  5. #5
    Newbie sthenri is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    15

    Hi

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. MySQL Loops
    By reachpradeep in forum Database & Database Programming
    Replies: 0
    Last Post: 03-04-2007, 09:12 AM
  2. JavaScript:Tutorial, Loops
    By TcM in forum Javascript
    Replies: 4
    Last Post: 12-13-2006, 08:53 AM
  3. JavaScript:Tutorial, Break Out Of Loops
    By TcM in forum Javascript
    Replies: 0
    Last Post: 12-08-2006, 05:35 AM
  4. Loops
    By Sionofdarkness in forum C and C++
    Replies: 7
    Last Post: 07-28-2006, 07:53 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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