2

I am trying to get user input for sortValues[] array using the for statement (enter character 1, enter character 2, etc).

However, when I execute this, the program will not allow me to enter for character 2, instead skipping directly to character 3, as seen below.

enter image description here

How to resolve this? The code is included below.

thanks!

static public void s_1d_char () {
            int counter=0;
            int x=0;
            c.print("How many characters? ");
            counter = readInt();

            char[] sortValues = new char[counter+1];

            for (x=1;x<=counter;x++) {
                    System.out.println("Enter character "+(x)+":");
                sortValues[x] = readChar();
            }
    }

readChar implementation (this is from a library):

public synchronized char readChar ()
{
char result, ch;

if (ungotChar != EMPTY_BUFFER)
{
    result = (char) ungotChar;
    ungotChar = EMPTY_BUFFER;
    return (result);
}

if (lineBufferHead != lineBufferTail)
{
    result = lineBuffer [lineBufferTail];
    lineBufferTail = (lineBufferTail + 1) % lineBuffer.length;
    return (result);
}

startRow = currentRow;
startCol = currentCol;
if (currentRow > maxRow)
{
    startRow++;
    currentCol = 1;
}

// Turn cursor on if necessary
consoleCanvas.setCursorVisible (true);

// Wait for a character to be entered
while (true)
{
    ch = getChar ();

    if (ch == '\n')
    {
    clearToEOL = false;
    if (echoOn)
        print ("\n");
    clearToEOL = true;
    lineBuffer [lineBufferHead] = '\n';
    lineBufferHead = (lineBufferHead + 1) % lineBuffer.length;
    break;
    }
    if (ch == '\b')
    {
    if (lineBufferHead == lineBufferTail)
    {
        consoleCanvas.invertScreen ();
    }
    else
    {
        int chToErase;

        lineBufferHead = (lineBufferHead + lineBuffer.length - 1) % lineBuffer.length;
        chToErase = lineBuffer [lineBufferHead];
        if (echoOn)
        {
        if (chToErase != '\t')
        {
            erasePreviousChar ();
        }
        else
        {
            int cnt;
            eraseLineOfInput ();
            cnt = lineBufferTail;
            while (cnt != lineBufferHead)
            {
            print (lineBuffer [cnt]);
            cnt = (cnt + 1) % lineBuffer.length;
            }
        }
        }
    }
    } // if backspace
    else if (ch == '\025')
    {
    if (echoOn)
    {
        eraseLineOfInput ();
    }
    lineBufferHead = lineBufferTail;
    }
    else
    {
    if (echoOn)
    {
        print (ch);
    }
    lineBuffer [lineBufferHead] = ch;
    lineBufferHead = (lineBufferHead + 1) % lineBuffer.length;
    }
} // while

result = lineBuffer [lineBufferTail];
lineBufferTail = (lineBufferTail + 1) % lineBuffer.length;

// Turn cursor on if necessary
consoleCanvas.setCursorVisible (false);

return (result);
}
13
  • 2
    could it be that pressing enter counts as the second char? Commented Jul 10, 2012 at 21:43
  • If this is the case, how do I resolve it? Commented Jul 10, 2012 at 21:45
  • can you please write the implementation of readChar()? Commented Jul 10, 2012 at 21:45
  • I don't know if it is the case, which is why im' making comments and not answers, but you can check by just not pressing enter between chars Commented Jul 10, 2012 at 21:45
  • If you use a BufferedInputStream to wrap your input, you have a handy readLine() method. Commented Jul 10, 2012 at 21:46

2 Answers 2

1

I recommend getting user input with a scanner:

import java.util.Scanner;

// ...
int counter = 0;

System.out.println("How many characters?");
Scanner keyboard = new Scanner(System.in);
counter = keyboard.nextInt();

char[] sortValues = new char[counter+1];

// Start your index variable off at 0
for (int x = 0; x < counter; x++) { 
  System.out.println("Enter character "+(x)+":");
  keyboard = new Scanner(System.in);
  String line = keyboard.nextLine();
  sortValues[x] = line.charAt(0);
}

This will capture the first character of the line. If the user enters more than one character, the program will read only the first. Also, you should really start your index variable x off at 0, considering arrays are 0-based indexed.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response. I have implemented your code, however, the program now stops after "How many characters" (it does not display Enter character, etc)...do you know the solution?
@01jayss: I updated the code snippet with a working solution.
1

instead of readChar() try:

sortValues[x] = Integer.parseInt(System.console().readLine());

How to read integer value from the standard input in Java

4 Comments

this only works for getting an integer... im trying to get a "char" value
@01jayss try maybe char c=System.console().readLine().charAt(0)
A char can be represented by an int. See docs. It's a 16-bit int, essentially, whereas the int data type is 32-bit.
@01jayss, what Roddy said, so long as you store it in a char (and maybe give it a cast), Java will automatically display it as a char.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.