how can i simplify my java code

how can i simplify my java code

/* 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 


  }

}

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
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
Advertisements
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.ADS_TO_REPLACE_1 Thanks
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
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.ADS_TO_REPLACE_1 Here is the sample
How can I find my DHCP IP address on my computer?
How can I find my DHCP IP address on my computer?  Hi, I am using Ubuntu and I trying to find the IP address assigned to my computer by DHCP server. How to find the IP address? Thanks   Hi, You can try following
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 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 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 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
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 start my career in data science?
How can I start my career in data science?  Hi, I am beginner... to learn: How can I start my career in data science? Try to provide me good examples or tutorials links so that I can learn the topic "How can I start my
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
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 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 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 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
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 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
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
Can I learn R on my own?
Can I learn R on my own?  Hi, I am beginner in Data Science... on my own? Try to provide me good examples or tutorials links so that I can learn the topic "Can I learn R on my own?". Also tell me which
Can I put coursera on my resume?
Can I put coursera on my resume?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: Can I... so that I can learn the topic "Can I put coursera on my resume?"
Can I put online courses on my CV?
Can I put online courses on my CV?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: Can... links so that I can learn the topic "Can I put online courses on my CV?"
Can I learn Hadoop on my own?
Can I learn Hadoop on my own?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: Can I... that I can learn the topic "Can I learn Hadoop on my own?". Also tell me
How should I start to learn code with Java?
How should I start to learn code with Java?  Hi, I want to learn... Java. Now I don't know where to start? Guide in learning Java and tell use... easy and you can just get started in one day. Follow our tutorial Learn Java
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
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
I need help on my Java code.... please please help me out!?
I need help on my Java code.... please please help me out!?  Well my code is supposed to ask for an input file and then (ex: input.txt), read... in the file. Also it should be displayed on the screen. However my code doesn't display
How can I learn Java in 5 minutes
How can I learn Java in 5 minutes  Hi, I am searching for the tutorials to learn Java programming. I don't know how easy or difficult is Java.... It will be helpful for me if someone gives me tutorials to learn Java. How can I
How can I master Java in one month?
How can I master Java in one month?  Hi, I wish to learn Java programming in one month. I have little experience in c programming and PHP programming. Is there any way to learn Java quickly. How can I master Java in one month
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
this is my code but i cant able to run it wt is the problem
this is my code but i cant able to run it wt is the problem  import java.io.*; class createthread { int thread=4,i; class thread1 extends createthread { public void run() { for(i=0;i
this is my code but i cant able to run it wt is the problem
this is my code but i cant able to run it wt is the problem  import java.io.*; class createthread { int thread=4,i; class thread1 extends createthread { public void run() { for(i=0;i
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
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 to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly
how to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly   index.html <... will display here</div> </body> </html> print("code sample
this is my javascript code and i am not understanding the mistake in this,please help me?
this is my javascript code and i am not understanding the mistake in this,please help me?  <html> <h2>Form Validation</h2> <script language = "Javascript"> function checkEmail
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 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
can i know the error in this code... am unable to run this code
can i know the error in this code... am unable to run this code  ...}; int n=arr.lenght; System.out.println("length of array:"+n); for(int i=0;i System.out.println(arr[i]); } }   You have used incorrect symbol in your
this is my code java - Date Calendar
this is my code java  /* * NewJFrame.java * * Created on 11...); #### This My problem jTextField3.setText(years); #### This My problem how... NOT modify this code. The content of this method is * always regenerated
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

Ads