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);
}
Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
01jayss
  • 1,290
  • 6
  • 18
  • 28

2 Answers2

1

instead of readChar() try:

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

How to read integer value from the standard input in Java

Community
  • 1
  • 1
J Max
  • 2,245
  • 2
  • 23
  • 42
  • this only works for getting an integer... im trying to get a "char" value – 01jayss Jul 10 '12 at 21:49
  • 1
    @01jayss try maybe `char c=System.console().readLine().charAt(0)` – Pshemo Jul 10 '12 at 21:51
  • A char can be represented by an int. See [docs](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). It's a 16-bit int, essentially, whereas the int data type is 32-bit. – Roddy of the Frozen Peas Jul 10 '12 at 22:22
  • @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. – ardent Jul 11 '12 at 06:46
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.

Chris Dargis
  • 5,203
  • 3
  • 33
  • 59
  • 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 Jul 10 '12 at 22:20
  • @01jayss: I updated the code snippet with a working solution. – Chris Dargis Jul 10 '12 at 23:55