Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

need help with java

Name: lazy motherfucker 2008-10-20 12:40

why doesn't this shit read the second string i ask it to read when you hit no. 2?
also, messages in spanish as it is for a college project and i live in some crappy southamerican country and i'm too lazy to change it

import java.io.*;
public class principal {
    public static void main (String args[]) {
        boolean menu=true;
        String textocodificar = null;
        String clavecodificacion = null;
        char opcion = '0';
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    while (menu == true) {
            System.out.println ("menu:");
            System.out.println ("        blah blah");
            System.out.print ("Enter a number (the only one that works is no. 2)");
            try {
                opcion = (char) System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (opcion == '2') {
                System.out.println ("Opción 2: Codificar mensaje");
                System.out.println ("Por favor, introduzca el texto a codificar: ");
                try {
                    textocodificar = bf.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.print ("Introduzca la clave: ");
                try {
                    clavecodificacion = bf.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println ("La Codificación Vinegère del texto dado es: ");
                System.out.println ("El texto 'mide' " + textocodificar.length());
                System.out.println ("La clave 'mide' " + clavecodificacion.length());
                try {
                    System.in.read();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (opcion == '4') {
                menu=false;
            }
        }
    }
}

Name: Anonymous 2008-10-20 12:48

     try {
                opcion = (char) System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }

Ok, imagine you type 2\n here. It reads the '2' into opcion, then it goes into opcion == '2' block. Now, when it hits
try {
                    textocodificar = bf.readLine();
                }

There's still a \n remaining on the input. You need to make it eat the \n when you read the value of opcion. Something like this:
   String opcionS = bf.readLine();
   opcion = opcionS.charAt(0);


In short, GET OUT

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List