Share on Google+Share on Google+

Patric Henning
how can i simplify my java code
1 Answer(s)      6 years and 5 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 


  }

}

Ads

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 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 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
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads