Results 1 to 5 of 5
  1. #1
    quemadissimo is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default New to the forum + assistance :)

    Hey everyone I'm new to the forum and I signed up because I'm new in Java programming and I am really stuck and I would like more advanced people to assist me.
    Thank you in advance :)

    The problem is the following: I try to compile, and even though NetBeans doesn't detect any error prior to running, when I do run it I get the following error:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - reached end of file while parsing
    at obligatorio.Prueba.main(Prueba.java:19)
    Java Result: 1

    Here's the code:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package obligatorio;
    import java.util.Scanner;
    public class Prueba {
        public static void main(String[] args) {
             Scanner input = new Scanner(System.in);
        Empresa E1;
        E1 = new Empresa();
        Empleado P1;
        P1 = new Empleado();
                    System.out.println("1: Registre una empresa");
            System.out.println("2: Registre un empleado");
            System.out.println("3: Registre cosa larga");
            int opcion = 0;
              
    do {   System.out.println("Seleccione una opcion: ");
                opcion = input.nextInt();
                boolean entrada = false;
    
            switch(opcion){
                case 1:{
                    opcion = 1;
                    System.out.println("Ingrese nombre: ");
                    E1.setNombre(input.next());
                    System.out.println("Ingrese rut: ");
                    E1.setRut(input.next());
                    break;}
                case 2:{
                    opcion = 2;
                    System.out.println("Ingrese nombre: ");
                    P1.setNombre(input.next());
                    System.out.println("Ingrese direccion: ");
                    P1.setDireccion(input.next());
                    System.out.println("Ingrese documente: ");
                    P1.setDocumento(input.next());
                    break;}
                case 3:{
                    opcion = 3;
                    System.out.println("Aca va la cosa larga");
                    break;}
            }
                    if(opcion == 1, 2, 3)){
    entrada = true
            {
                while (entrada = true){
                }
            
    
    }
    Can you guys tell what's wrong with the programming? It's most likely a beginner's mistake! Thanks again!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Your braces have to "match". What I mean is that for each { there has to be a later }. I think you may be missing a }.

    Note that you don't need to put braces around the blocks of code that make up each of the cases. You can just say:

    Java Code:
    switch(opcion) {
        case 1:
            opcion = 1;
            System.out.println("Ingrese nombre: ");
            E1.setNombre(input.next());
            System.out.println("Ingrese rut: ");
            E1.setRut(input.next());
            break;
        case 2:
            opcion = 2;
            // etc
    Also the line "opcion = 1;" is not really needed because you wouldn't get there unless the user had entered 1 and opcion would already be 1.

    Java Code:
    if(opcion == 1, 2, 3)) {
    This doesn't make sense. Perhaps you should read up about conditional operators to see how you can express the condition "opcion is 1 or opcion is 2 or opcion is 3".

    Finally use lower case letters to start variables. And make the variables readable even if that makes them a little longer.

    Java Code:
    //Empresa E1;
    //E1 = new Empresa();
    Empresa empresa = new Empresa();
    Last edited by pbrockway2; 10-31-2009 at 03:20 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I haven't addressed the "while(entrada=true){}" business because these other things should be corrected first.

    And then you can say what this while loop was supposed to do.

  4. #4
    quemadissimo is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    okay thank you, I seem to have fixed the problem and it runs correctly now.
    The code is now as follows:
    Java Code:
    import java.util.Scanner;
    public class Prueba {
        public static void main(String[] args) {
             Scanner input = new Scanner(System.in);
        Empresa e1;
        e1 = new Empresa();
        Empleado p1;
        p1 = new Empleado();
                    System.out.println("1: Registre una empresa");
            System.out.println("2: Registre un empleado");
            System.out.println("3: Registre cosa larga");
            int opcion = 0;
            boolean entrada = false;
    
            while(entrada = true){
        System.out.println("Seleccione una opcion: ");
        opcion = input.nextInt();
        
    
        
        switch(opcion){
           case 1:
                           System.out.printf("Ingrese nombre: ");
                    e1.setNombre(input.next());
                    System.out.printf("Ingrese rut: ");
                    e1.setRut(input.next());
                    entrada = true;
                    System.out.println(e1);
                    break;
                case 2:
                    opcion = 2;
                    System.out.printf("Ingrese nombre: ");
                    p1.setNombre(input.next());
                    System.out.printf("Ingrese direccion: ");
                    p1.setDireccion(input.next());
                    System.out.printf("Ingrese documento: ");
                    p1.setDocumento(input.next());
                    entrada = true;
                    System.out.println(p1);
                    break;
                case 3:
                    opcion = 3;
                    System.out.printf("Aca va la cosa larga");
                    entrada = true;
                    break;
            }
            }
        }
    }
    The only problem I seem to be having now is with the input.next(); as it takes only the first word typed as the value when the value should be more than one word in direccion (spanish for address), for instance, which should have a number and a street name. The code as it is now uses the first word as the value for the first set* and the second one for the following one. How do I make input.next() use everything the user types until they hit enter as the value for the first setting?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    next reads the next token. If your delimiter is space (the default) then you only get one word read at a time. Use nextLine to read the whole line.

Similar Threads

  1. Looking for assistance
    By s_dawg101 in forum New To Java
    Replies: 32
    Last Post: 11-04-2009, 03:49 AM
  2. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 05:14 PM
  3. X-Tremely new to this...Need assistance...
    By Johnny562 in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 10:17 PM
  4. [SOLVED] Your assistance required please!
    By crazydeo in forum New To Java
    Replies: 1
    Last Post: 05-21-2008, 12:58 PM
  5. Welcome to our new forum: Forum Lobby
    By JavaForums in forum Forum Lobby
    Replies: 18
    Last Post: 02-07-2008, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •