Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners how can i simplify my java code

Have Programming Question? Ask it here!
 
 
 


Patric Henning
how can i simplify my java code
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


/* Create a program that prompts the user to enter a password. Display a suitable message for each correct or incorrect attempt made. The user must be given three tries after which the program should terminate. */ import java.util.*; import java.util.Random; import javax.swing.*;

public class PatricHenningspassword

{ public static void main (String [] args) { Scanner kb = new Scanner (System.in); String pass=""; int c = 1; int code=0; String tryer; String out= rev_num();

     while (!pass.equals(out) && c<=3)
     {
        c++;
        System.out.println("password is : "+out);
        System.out.println(" Please enter password");
        pass = kb.next();
        if (pass == null)
           System.exit(0);

        if (pass.equals(out))

        {
           System.out.println("Well Done You Right on Target");  
        }            
        else
        {
           if (c<=2 && !pass.equals(out))
           {   System.out.println("nice try sorry \n \n");
      System.out.println("Enter password its your second last chance!!!!");
              pass = kb.next();
              c++;
              if (pass.equals(out))
              {
                 System.out.println("Well Done You Right on Target");  
              } 
              else
              { if (c==3 && !pass.equals(out))
                {
             System.out.println("nice try sorry last try \n \n");
             System.out.println("Enter password its your last chance!!!!");
                   pass = kb.next();
                   c++;
                 if (pass.equals(out))
                 {
                 System.out.println("Well Done You Right on Target");  
                 } 


                 else 
                 {
                    c++;
                    System.out.println("Access Denied !!!!!!!!!");    

                    System.exit(0);
                 }  

              }           
           }


           }
        }
     }
  }


   public static String rev_num()
  {
    int a=0;int d=0;int g=0;int j=0;
     int b=0;int e=0;int h=0;
     int c=0;int f=0;int i=0;

     int counter=0;

     int starter=0;
     int cot=1;

     for (int y=9;y<2;)
     {
        counter--;
     } 
     for (int z=7;z<=4;)
     {
        z--;
       cot++;

        starter=(int)(Math.random() *counter +1);
     } 
     String outr="";



     a = (int)(Math.random() * 9 +counter);
     b = (int)(Math.random() * 9 +starter);
     c = (int)(Math.random() * 9 +cot);
     d = (int)(Math.random() * 9 +counter);
     e = (int)(Math.random() * 9 +1);
     f = (int)(Math.random() * 9 +starter);
     g = (int)(Math.random() * 9 +cot);
     h = (int)(Math.random() * 9 +counter);
     i = (int)(Math.random() * 9 +starter);
     j = (int)(Math.random() * 9 +cot);

     String k=Integer.toString(a); String l=Integer.toString(b); String m=Integer.toString(c);
     String n=Integer.toString(d); String o=Integer.toString(e); String p=Integer.toString(f);
     String q=Integer.toString(g); String r=Integer.toString(h); String s=Integer.toString(i);
     String t=Integer.toString(i);




     outr=k+l+m+n+o+p+q+r+s+t;
     return outr;
        //cannot return again; illegal 


  }

}


Advertisement
View Answers

November 15, 2010 at 2:51 PM


Hi Friend,

Try the following code:

import java.util.*;
import javax.swing.*;

public class PatricHenningspassword{ 
    public static void main (String [] args) { 
        Scanner kb = new Scanner (System.in);
        String pass="";
        int c = 1;
        int code=0;
        String tryer;
        String out= rev_num();
     while (!pass.equals(out) && c<=3){
        c++;
        System.out.println("password is : "+out);
        System.out.println(" Please enter password");
        pass = kb.next();
        if (pass == null)
           System.exit(0);
        System.out.println();
        if (pass.equals(out)){
           System.out.println("Congratulations!");  
        }            
        else{
           System.out.println();
           if (c<=2 && !pass.equals(out)){  
              System.out.println("Invalid Password! Try Again!");
              pass = kb.next();
              c++;
              if (pass.equals(out)){
                 System.out.println("Congratulations");  
              } 
              else{
              if (c==3 && !pass.equals(out)){
             System.out.println();
             System.out.println("Invalid Password! Try Again!");
             System.out.println("**Note! This is your last attempt!");
                   pass = kb.next();
                   c++;
                  System.out.println();
                 if (pass.equals(out)){
                 System.out.println("Congratulations!");  
                 } 
                 else{
                    c++;
                    System.out.println("Access Denied !!!!!!!!!");    
                    System.exit(0);
                 }  
               }           
             }
           }
        }
     }
  }
  public static String rev_num(){
     int a=0;int d=0;int g=0;int j=0;
     int b=0;int e=0;int h=0;
     int c=0;int f=0;int i=0;
     int counter=0;
     int starter=0;
     int cot=1;
     for (int y=9;y<2;){
        counter--;
     } 
     for (int z=7;z<=4;){
       z--;
       cot++;
     starter=(int)(Math.random() *counter +1);
     } 
     String outr="";
     a = (int)(Math.random() * 9 +counter);
     b = (int)(Math.random() * 9 +starter);
     c = (int)(Math.random() * 9 +cot);
     d = (int)(Math.random() * 9 +counter);
     e = (int)(Math.random() * 9 +1);
     f = (int)(Math.random() * 9 +starter);
     g = (int)(Math.random() * 9 +cot);
     h = (int)(Math.random() * 9 +counter);
     i = (int)(Math.random() * 9 +starter);
     j = (int)(Math.random() * 9 +cot);
     String k=Integer.toString(a); String l=Integer.toString(b); String m=Integer.toString(c);
     String n=Integer.toString(d); String o=Integer.toString(e); String p=Integer.toString(f);
     String q=Integer.toString(g); String r=Integer.toString(h); String s=Integer.toString(i);
     String t=Integer.toString(i);

     outr=k+l+m+n+o+p+q+r+s+t;
     return outr;
    }
}

Thanks



Related Tutorials/Questions & Answers:
how can i simplify my java code
how can i simplify my java code  /* Create a program that prompts... Scanner (System.in); String pass=""; int c = 1; int code=0... c=0;int f=0;int i=0; int counter=0; int starter=0; int cot=1
How can I to my database to my application
How can I to my database to my application  How can I to my database to my application   Hi, Please see the JDBC discussion thread. Thanks
Advertisements
How i can send testing mail on my id using java?
How i can send testing mail on my id using java?  Which packages i should use to implementing java mail service??? Thanks
My Base Class is Changing every time in my code. How I can overcome this?
My Base Class is Changing every time in my code. How I can overcome this?  I have had the below question asked in interview, i'm curious to learn the answer. I have a base class, 'GeneratorBaseClass' that is extended
How can I protect my database password ?
How can I protect my database password ?   How can I protect my database password ? I'm writing a client-side java application that will access... in as plain text. What can I do to protect my passwords
How can i modify my account in roseindia
How can i modify my account in roseindia  Presently am not using my gmail id. I have to modify my roseindia account. Please send the answer to following mail id
How can I connect my database to my application ?
How can I connect my database to my application ?  How can I connect my database to my application?   Hi, You can use JDBC API to connect to database from your Java application. Here is the sample code to connect
How can I open my Java desktop app by clicking on its file?
How can I open my Java desktop app by clicking on its file?  How can I open my Java desktop app by clicking on the file it creates? This app creates... for this app. For instance, imagine I create an editor app called BlueSky
How can i add a new count to this source code ? - Java Beginners
How can i add a new count to this source code ?  Hi everyone I...;ZHTYPE (Just Logical) Please tell me how can i see this output? Code... with this code is true but I want add ZHTYPE at the end of each line + XTSM/XTS please
How can i add a new count to this source code ? - Java Beginners
How can i add a new count to this source code ?  Hi everyone I...;ZHTYPE (Just Logical) Please tell me how can i see this output? Code... This output with this code is true but I want add ZHTYPE at the end of each line
how i can add an horizontal scrollbar at my PdfAnnotation ? - JSP-Servlet
how i can add an horizontal scrollbar at my PdfAnnotation ?  How to add the an horizontal scrollbar at my PdfAnnotation ? for example document d=new document(0. document.open
how can i use toggle button in my application
how can i use toggle button in my application  i want a on/off toggle button by dragging left-right..not by pressing on button
my hibernate showing the following exception so how i can resolve it
my hibernate showing the following exception so how i can resolve it   Caused by: java.lang.ClassNotFoundException: org.hibernate.Session at java.net.URLClassLoader$1.run(Unknown Source
How can I learn Java?
How can I learn Java?  Hi, I have just completed a course in HTML.... Now I want to learn Learn Java programming and become the expert in Java programming. How can I learn Java? in shortest possible time. I mean I just want to begin
I want to change my user name on your website ,how can i do this
I want to change my user name on your website ,how can i do this  I want to change my user name on your website ,how can i do this   You can create another account very easily with different username
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
How can I Convert my Image Files to Text Files? - IDE Questions
How can I Convert my Image Files to Text Files?  How can I Convert my Image Files to Text Files
how can i close a frame. - Java Beginners
how can i close a frame.  Hi, My question is how can we close...............my target is when i click on that button, a new frame is coming and updated table... frames as i go on clicking button...........can we have like when i click on button
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers  how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
How can i implement the calculator programe in jsp code
How can i implement the calculator programe in jsp code  Please send code for the calculator using jsp technologies please guidelines to me.   Here is a simple jsp calculator code that accepts two numbers from the user
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need help creating a mysql database for this code. code is import java.awt....";, "root", "root"); Statement st=con.createStatement(); int i
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need help creating a mysql database for this code. code is import java.awt....";, "root", "root"); Statement st=con.createStatement(); int i
i want to find the byte code of a image file ... for my project..plz if anybody help me for java coding i will grateful..
i want to find the byte code of a image file ... for my project..plz if anybody help me for java coding i will grateful..   i want to convert... project.. but i cant convert Image file to its byte code format.. if anybody can plz
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
how can i connected jsp wih mysql - Java Beginners
how can i connected jsp wih mysql  i have a code jsp file and connection file connection file will be compiling successful. i m using session in my... to connect this and how can i use it plz help me its very urgent plz send my
i am unable to identify the error in my code
i am unable to identify the error in my code  class Program { public static void main(String[] args) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter
i am unable to identify the error in my code
i am unable to identify the error in my code  class Program { public static void main(String[] args) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter
how can i achieve multiple inheritance in java without using inheritance ?
how can i achieve multiple inheritance in java without using inheritance ?  how can i achieve multiple inheritance in java without using inheritance
how can i make a session invalidate in java when browser is closed
how can i make a session invalidate in java when browser is closed   how can i make a session invalidate in java when browser is closed   as per my understanding sessions automatically expires once browser is closed
how can i create a data grid - Java Beginners
how can i create a data grid  Nice to ur respose...thnks all of u plz help me i want make a datagrid but i have no idea about this how can i create datagrid. and how can i connect to the database plz help me give
how can i draw a table using applet or swings - Java Beginners
how can i draw a table using applet or swings  Hi RoseIndia, Actually, I was trying for creating a table using applet or swings.......... My task is create a table and enter the integer values during run time......I have
How can i read a file from nth line onwards in java
How can i read a file from nth line onwards in java  Hi, I need to read a file content from its nth line onwards till end. How can i implement it in java. Regards, Chinnu
How can i download these java related materials from rose india
How can i download these java related materials from rose india  How can i download these java related materials from rose india   Hello Friend, In every section of the website, there is download option. Just go
How I can Set Transparent Background Image to Panel - Java Beginners
How I can Set Transparent Background Image to Panel  Sir ,How I can Set Transperent Background Image to Jpanel. plz Help Me
how i open my web page
how i open my web page   how i open my web page on different broadband machine if i don't have any domain name... i m using a broadband .........i... localmachine/localhost:8080/app/app.htm so tell me step how i open my page
Can i write JavaScript code in AjaxResponse Code ?
Can i write JavaScript code in AjaxResponse Code ?  Hai Every Dynamic's We can't write JavaScript code in Ajax Response Code.Why because it takes...'s not working.rather than that i try to created on innerHtml document. here
How can i write a datagrid coding - Java Beginners
How can i write a datagrid coding  Hi, I want make a datagrid..plz send me datagrid coding using mysql database and jsp..plz tell me brief description about grid how can i connect datagrid with database. Thanks
how can i close particular frame in swings - Java Beginners
how can i close particular frame in swings  Hi, I want to know, how can we close a particular frame when mulitple frames are appearing as output... in swings and three frames are having their own close buttons and when i click
How could I use Socket in my application?
How could I use Socket in my application?  I created an application dealing with the management of a supermarket using Hibernate and Swing. I found out today that I have to use also Socket and I don't know exactly what I could do
how can i calculate loan - Java Interview Questions
how can i calculate loan  negotiating a consumer loan is not always straightforward.one form of loan is the discount installment loan, which works as follows. suppose a loan has a face value of 1,000 by 0.15 to yield 225
How can I do it? .click();
How can I do it? .click();  I have a very unusual problem. I want...("a"); x.click(); </script> So it's click on an element witch one Id's is "a", but I want that it make mouseup in this element. How can I do it, because if I write
how should i can solve
how should i can solve  Web based program - Input - Person's contact details with Passport Number as Unique Key. Save data in to oracle / MySQL. Output - List of Persons saved in the database. Technology to be used - JSP
In JSP page, inside a javascript function, can I place a java code using scriptlet tag?
In JSP page, inside a javascript function, can I place a java code using... to checkListValue() method using scriptlet tag. My doubt here is, can I call the method... is trying to close a window, I am calling a java script function closeWind
I developed a simple java web project in Struts now If I have to import the project in Eclipse Indigo how can I do it
I developed a simple java web project in Struts now If I have to import the project in Eclipse Indigo how can I do it   I developed a simple java web project in Struts now If I have to import the project in Eclipse Indigo how
I need add my java program with a picture.
I need add my java program with a picture.  Good evng Frnds Friends i created 1 jar file in my desktop. I need add this program with 1 picture... of opening Picture. I think most of the softwares are like that . They are first
Which java can i download?
Which java can i download?  Hello, i'm a beginner on java. Which java can i download for to exercise with my codes? Thanks in advance.   nobody.   And i download Eclipse java. But when i want to install
 

 

 

DMCA.com