Results 1 to 6 of 6
  1. #1
    javajunior is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Java : Begginer`s excercise

    From this code:
    Java Code:
    String usuario="pepito", clave="educacionit";
    if usuario= "pepito" and clave="educaionit" inform "W elcome pepito"

    if usuario= "pepito" and clave isn´t "educaionit" inform "the user doesnt match with the passsword"

    if usuario no es "pepito" inform "user doesnt exist"


    the problem is when i had is that i put the user and the password right but then in de command line of netbeans throw an error:

    Java Code:
    Ingrese usuario: 
    pepito
    Ingrese clave: 
    educaionit
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:864)
    	at java.util.Scanner.next(Scanner.java:1485)
    	at java.util.Scanner.next(Scanner.java:1418)
    	at ejercicio3.Ejercicio3.main(Ejercicio3.java:25)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 22 seconds)

    And this is my code
    Java Code:
    package ejercicio3;
    import java.util.Scanner;
    
    /**
     *
     * @author User
     */
    public class Ejercicio3 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner tec =new Scanner(System.in);
           
           System.out.println("Ingrese usuario: ");
           usuario=tec.next(usuario);
           System.out.println("Ingrese clave: ");
           clave=tec.next(clave);
           
           if(usuario=="pepito" && clave == "educacionit"){
               System.out.println("Ingrese usuario: ");
               usuario=tec.next(usuario);
               System.out.println("Ingrese clave: ");
               clave=tec.next(clave);
               System.out.println("Bienvenido pepito");
           }
           else if(usuario=="pepito" && clave != "educaionit"){
               System.out.println("Ingrese usuario: ");
               usuario=tec.next(usuario);
               System.out.println("Ingrese clave: ");
               clave=tec.next(clave);
               System.out.println("El usuario no coincide con la contraseña");
            }
           else if(usuario!="pepito"){
               System.out.println("Ingrese usuario: ");
               usuario=tec.next(usuario);
               System.out.println("Ingrese clave: ");
               clave=tec.next(clave);
                       
               System.out.println("El usuario no esxiste");
           }   
        
        }
        
        
        
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java : Begginer`s excercise

    Why does the code use an arg with the next() method?

    Does the posted code compile without errors? Where is the variable: usuario defined?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java : Begginer`s excercise

    Why are you trying to compare strings for (in)equality with the == and != operators? Did your text book tell you so, or did you assume Java would be similar to the Basic language, or did you make it up and you're hoping for the best?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    javajunior is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Java : Begginer`s excercise

    Hello thank you for your response .

    i did define usuario and clave a made a a mistake when i copy and paste the code here.. in the book doesnt even mention the class scanner i just looked in google..
    i tried to compare with equals :

    Java Code:
    package ejercicio3;
    import java.util.Scanner;
     
    /**
     *
     * @author User
     */
    public class Ejercicio3 {
     
       
        public static void main(String[] ar) {
            // TODO code application logic here
            Scanner tec =new Scanner(System.in);
            String usuario="pepito", clave="educacionit";
           System.out.println("Ingrese usuario: ");
           usuario=tec.next(usuario);
           System.out.println("Ingrese clave: ");
           clave=tec.next(clave);
            
           if(true){
           
           
           if("pepito".equals(usuario) &&  "educacionit".equals(clave)){
               
               System.out.println("Bienvenido pepito");
           }
           else{ if("pepito".equals(usuario)&& !"educaionit".equals(clave)  ){
               
               System.out.println("El usuario no coincide con la contraseña");
                          }
           }
            if(!"pepito".equals(usuario) && "educacionit".equals(clave) ){
               
                        
               System.out.println("El usuario no esxiste");
           }  
         
        }
         
         
        }
    }
    and the result:

    Ingrese usuario:
    pepito
    Ingrese clave:
    educacionit
    Bienvenido pepito
    BUILD SUCCESSFUL (total time: 14 seconds)
    but when i put wrong the pass:

    Ingrese usuario:
    pepito
    Ingrese clave:
    adfdf
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.next(Scanner.java:1418)
    at ejercicio3.Ejercicio3.main(Ejercicio3.java:18)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 21 seconds)

    or the user:

    Ingrese usuario:
    sdfs
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.next(Scanner.java:1418)
    at ejercicio3.Ejercicio3.main(Ejercicio3.java:16)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 9 seconds)
    Last edited by Norm; 04-06-2014 at 11:52 PM. Reason: Changed quote tags to code

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java : Begginer`s excercise

    Please use code tags for the code, not quote tags.

    Why does the code use an arg with the next() method?
    Java Code:
     usuario=tec.next(usuario);
    // vs
     usuario=tec.next();
    Last edited by Norm; 04-06-2014 at 11:54 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    javajunior is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Java : Begginer`s excercise

    Thank you Norm and Jos , solved..

Similar Threads

  1. Need help with excercise
    By lodestaryouth in forum New To Java
    Replies: 4
    Last Post: 03-14-2014, 10:00 AM
  2. Best place/way to learn java for a begginer
    By yakovha in forum Forum Lobby
    Replies: 1
    Last Post: 06-10-2013, 08:34 PM
  3. What are some cool begginer things to do with Java?
    By CiniCraft in forum New To Java
    Replies: 2
    Last Post: 09-11-2011, 11:13 PM
  4. Replies: 17
    Last Post: 08-22-2010, 05:34 AM
  5. Replies: 2
    Last Post: 03-18-2010, 01:39 PM

Tags for this Thread

Posting Permissions

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