7 Replies - 2965 Views - Last Post: 07 November 2010 - 01:34 PM Rate Topic: -----

#1 sunboy_08282  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 01-November 10

comparing and counting characters in sentence

Posted 07 November 2010 - 10:02 AM

hi all,

I have two sentences in two line and I want to reach each character of sentence and find out the number of character. for example:
Sentence 1: aaabba
Sentence 2: ababbb

I want to read the first line and compare if first character is equal to 'a' or not and continue it till it reaches to 'b' and count the number of character 'a'. In this example I want the output be like
3 2 2 1 1 3

I'm a little confused with code and method that I could use, one way is to read character by character and store each in array and then compare arrays with each other and put a counter to get me the number, but I want to know that is there any method that read the file character by character and compare character with the specific one. if there is a method for that I think after that for counting the number I only need to put a counter.

tx

Is This A Good Question/Topic? 0
  • +

Replies To: comparing and counting characters in sentence

#2 Kurplepush  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 08-March 10

Re: comparing and counting characters in sentence

Posted 07 November 2010 - 11:34 AM

I think you can just use the nextChar() method of the Scanner class
Was This Post Helpful? 0
  • +
  • -

#3 sunboy_08282  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 01-November 10

Re: comparing and counting characters in sentence

Posted 07 November 2010 - 12:16 PM

View PostKurplepush, on 07 November 2010 - 05:34 PM, said:

I think you can just use the nextChar() method of the Scanner class


tx, but it is not useful. Please help with the exact code,tx

This post has been edited by sunboy_08282: 07 November 2010 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: comparing and counting characters in sentence

Posted 07 November 2010 - 12:34 PM

View PostKurplepush, on 07 November 2010 - 02:34 PM, said:

I think you can just use the nextChar() method of the Scanner class

Actually, there is no nextChar() method for the Scanner class. You can read a String though, and use the charAt() method for Strings.

@sunboy_08282: You shouldn't be interested in the individual values in the String, just whether or not consecutive values are the same. So what I would do is read in all the lines and store them as one String. So then I would use the following algorithm:
-Store the first character in the String in a temp variable
-Set a counter to 1
-for each character from index 1 to the end
      -if it is the same as the temp
              -Increment the counter
      -else 
              -Print the counter
              -Set temp to the new character
              -Reset the counter to 1



We are happy to further help you with your good faith efforts, but we will not simply give you the code. :)
Was This Post Helpful? 0
  • +
  • -

#5 sunboy_08282  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 01-November 10

Re: comparing and counting characters in sentence

Posted 07 November 2010 - 01:02 PM

View Postmacosxnerd101, on 07 November 2010 - 06:34 PM, said:

View PostKurplepush, on 07 November 2010 - 02:34 PM, said:

I think you can just use the nextChar() method of the Scanner class

Actually, there is no nextChar() method for the Scanner class. You can read a String though, and use the charAt() method for Strings.

@sunboy_08282: You shouldn't be interested in the individual values in the String, just whether or not consecutive values are the same. So what I would do is read in all the lines and store them as one String. So then I would use the following algorithm:
-Store the first character in the String in a temp variable
-Set a counter to 1
-for each character from index 1 to the end
      -if it is the same as the temp
              -Increment the counter
      -else 
              -Print the counter
              -Set temp to the new character
              -Reset the counter to 1



We are happy to further help you with your good faith efforts, but we will not simply give you the code. :)


thank you, before I saw your comment I worked on it and reach to this code, but there is sth wrong with it, actually this code read the file line by line, but I want to read from first character to the last character of file not the last character of each line

        while(fileScanner.hasNext()){
            line = fileScanner.next();
            System.out.println(line);
            for(int i=0;i<line.length();i++){
                if(line.charAt(i)=='-'){
                    count = count +1;
                    dashCount = dashCount + count;
                  
                      
                }
                else if (line.charAt(i)== '#'){
                    count = count +1;
                    hashCount = hashCount + count;
                }
                count = 0;
          //    System.out.print(dashCount + "  :test :"); 
            }
             System.out.print(dashCount + "  :test:");
             dashCount = 0;
        }


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: comparing and counting characters in sentence

Posted 07 November 2010 - 01:08 PM

I would read in all the lines of the File first and append them all to a single String, not calculate as you read line by line. You can use the Scanner nextLine() method to read in the entire line, rather than the next() method for an individual token.
Was This Post Helpful? 0
  • +
  • -

#7 sunboy_08282  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 01-November 10

Re: comparing and counting characters in sentence

Posted 07 November 2010 - 01:30 PM

View Postmacosxnerd101, on 07 November 2010 - 07:08 PM, said:

I would read in all the lines of the File first and append them all to a single String, not calculate as you read line by line. You can use the Scanner nextLine() method to read in the entire line, rather than the next() method for an individual token.


tx, now I can see, actually I should do it from first, reading the file and storing it in 1 string variable is the best way, but now I'm confused, as I am a beginner in java, at this moment I can't think correctly. how could I do that? I should do it before the while loop. shall I use this code?
String s = fileScanner.next();



using this code before while loop?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: comparing and counting characters in sentence

Posted 07 November 2010 - 01:34 PM

The next() method only reads in a single token, not the entire file. You will have to loop while hasNextLine(), read in the nextLine() and append it to the String. Then evaluate that String after that while loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1