Munzer
Munzer

Reputation: 2318

problems with reading input and storing it in an array

Hi I have an algorithm which should read user input save it in an array and count letters used A used 3 b used 3 ..... but my algorithm does not work with 1 , 3 , any odd number and work with 2, 4, even numbers.. Any ideas how to fix this error Thanks guys for help code:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int i = 0;
    char c;
  int counta = 0;
        int countb = 0;
        int countc = 0;
        int countd = 0;
        char [] array = new char [4];
        array[0]='a';
        array[1]='b';
        array[2]='c';
        array[3]='d';

    while (reader.read()!='\n') {
        int x = 1+i;
        char[] cbuf = new char[x];
        c = (char) reader.read();
        cbuf[i] = c;

               if (cbuf[i]==array[0]){
               counta++;
               }
               if (cbuf[i]==array[1]){
               countb++;
               }
               if (cbuf[i]==array[2]){
               countc++;
               }
               if (cbuf[i]==array[3]){
               countd++;
               }
          System.out.println(cbuf);

                i++;

        }

Upvotes: 0

Views: 61

Answers (5)

Edwin Torres
Edwin Torres

Reputation: 2864

Each time you call read(), it will accept a character from input. So store it in your variable c, and only call it when you're ready to accept another character (i.e. at the end of your loop). Also remember to check for an IOException:

try {
    c = (char)reader.read();  //read a character

    while (c != '\n') {   //check the character
        int x = 1+i;
        char[] cbuf = new char[x];
        cbuf[i] = c;

        if (cbuf[i]==array[0]){
            counta++;
        }
        if (cbuf[i]==array[1]){
            countb++;
        }
        if (cbuf[i]==array[2]){
            countc++;
        }
        if (cbuf[i]==array[3]){
            countd++;
        }
        System.out.println(cbuf);

        i++;
        c = (char)reader.read(); //ready to read another character
    }
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

WoDoSc
WoDoSc

Reputation: 2618

There are several problems in your code.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
char c;
int counta = 0;
int countb = 0;
int countc = 0;
int countd = 0;
char[] array = new char[4];
array[0] = 'a';
array[1] = 'b';
array[2] = 'c';
array[3] = 'd';

String line = new String();
while ((c = (char) reader.read()) != '\n') {

    if (c == array[0]) {
        counta++;
    }
    if (c == array[1]) {
        countb++;
    }
    if (c == array[2]) {
        countc++;
    }
    if (c == array[3]) {
        countd++;
    }
    line += c;
}
System.out.println(line);
System.out.println("a: " + counta);
System.out.println("b: " + countb);
System.out.println("c: " + countc);
System.out.println("d: " + countd);

This is simplier, and at the and it shows the string entered by the user and the count for each char.

Upvotes: 0

OmegaDirective
OmegaDirective

Reputation: 366

You are calling reader.read() twice, once in the loop conditional and then to assign to 'c'. The first time results in the buffer being read and 'wasted', therefore ~half the bytes are skipped. You may define 'c' outside the loop and then use

while ((c = reader.read()) != '\n') {

On a non-related note, consider using an array of 'counts' to hold the counts - it's usually the preferred way over defining four separate variables (counta, countb, countc, countd will be replaced with an int array counts[4].) You could then do:

counts[c-'a']++;

to automatically update the counts of the letters 'a', 'b', 'c', and 'd' (assuming that's the only valid input). This one line will replace the four 'if' statements!

Good luck!

Upvotes: 0

ajb
ajb

Reputation: 31689

Each time your program executes reader.read(), it will read a character. In your while loop, it executes reader.read() every time it loops back, so that it can check for \n. But you don't save the result; when you then say

`c = reader.read()`;

it reads a new character; it doesn't use the one that it already tested in the while expression.

You need to arrange things to that it only reads once. Two possibilities (there are others):

while ((c = reader.read()) !='\n') {
    // other code
    // GET RID OF THIS LINE c = (char) reader.read();

or

while (true) {
    c = (char) reader.read();
    if (c == '\n') {
        break;
    }
    // other code
    // and so on

This won't fix other errors in your code; it's just showing you a couple ways you can avoid calling a function twice in this kind of situation.

Upvotes: 0

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

The problem arise since you are reading twice on every iteration.

c = (char) reader.read();
while (c!='\n') {
    int x = 1+i;
    char[] cbuf = new char[x];
    cbuf[i] = c;

           if (cbuf[i]==array[0]){
           counta++;
           }
           if (cbuf[i]==array[1]){
           countb++;
           }
           if (cbuf[i]==array[2]){
           countc++;
           }
           if (cbuf[i]==array[3]){
           countd++;
           }
      System.out.println(cbuf);

            i++;

    c = (char) reader.read(); //read once on every iteration

    }

Upvotes: 2

Related Questions