01jayss
01jayss

Reputation: 1449

Java for loop error

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);
}

Upvotes: 2

Views: 183

Answers (2)

Chris Dargis
Chris Dargis

Reputation: 6043

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.

Upvotes: 1

J Max
J Max

Reputation: 2381

instead of readChar() try:

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

How to read integer value from the standard input in Java

Upvotes: 1

Related Questions