our program. I am still learning. Can you correct my program if its i don't use inheritance. Thanks. import java.io.*; import java.util.*; public class MyClass { public static void main(String args[]) { String uname,passw; Scanner sc=new Scanner(System.in); while(true){ System.out.println("1.Registration"); System.out.println("2.Login"); System.out.println("3.Exit"); System.out.print("Choose an option: "); int ch=sc.nextInt(); if(ch==1){ System.out.print("Enter your username: "); uname=sc.next(); while(true){ if(uname.matches("[a-zA-Z0-9]+")) break; else{ System.out.println("Only alphanumerics are allowed"); System.out.print("Enter your username: "); uname=sc.next(); } } System.out.print("Enter your password: "); passw=sc.next(); while(true){ if(passw.matches("[a-zA-Z0-9]+")) break; else{ System.out.println("Only alphanumerics are allowed"); System.out.print("Enter your password: "); passw=sc.next(); } } try { BufferedWriter out = new BufferedWriter( new FileWriter("records.txt", true)); out.write(uname+" "+passw); out.close(); } catch (IOException e) { System.out.println("exception occurred" + e); } } else if(ch==2){ System.out.print("Enter username: "); uname=sc.next(); System.out.print("Enter password: "); passw=sc.next(); try { FileInputStream fis=new FileInputStream("records.txt"); Scanner st=new Scanner(fis); int flag=1; while(st.hasNextLine()) { String s=st.nextLine(); String[] d=s.split(" "); if(d[0].equals(uname) && d[1].equals(passw)){ System.out.println("Successfully logged in"); flag=0; break; } } st.close(); if(flag==1) System.out.println("Incorrect username or password"); } catch(IOException e) { e.printStackTrace(); } } else if(ch==3) break; } } }
our program. I am still learning. Can you correct my program if its i don't use inheritance. Thanks. import java.io.*; import java.util.*; public class MyClass { public static void main(String args[]) { String uname,passw; Scanner sc=new Scanner(System.in); while(true){ System.out.println("1.Registration"); System.out.println("2.Login"); System.out.println("3.Exit"); System.out.print("Choose an option: "); int ch=sc.nextInt(); if(ch==1){ System.out.print("Enter your username: "); uname=sc.next(); while(true){ if(uname.matches("[a-zA-Z0-9]+")) break; else{ System.out.println("Only alphanumerics are allowed"); System.out.print("Enter your username: "); uname=sc.next(); } } System.out.print("Enter your password: "); passw=sc.next(); while(true){ if(passw.matches("[a-zA-Z0-9]+")) break; else{ System.out.println("Only alphanumerics are allowed"); System.out.print("Enter your password: "); passw=sc.next(); } } try { BufferedWriter out = new BufferedWriter( new FileWriter("records.txt", true)); out.write(uname+" "+passw); out.close(); } catch (IOException e) { System.out.println("exception occurred" + e); } } else if(ch==2){ System.out.print("Enter username: "); uname=sc.next(); System.out.print("Enter password: "); passw=sc.next(); try { FileInputStream fis=new FileInputStream("records.txt"); Scanner st=new Scanner(fis); int flag=1; while(st.hasNextLine()) { String s=st.nextLine(); String[] d=s.split(" "); if(d[0].equals(uname) && d[1].equals(passw)){ System.out.println("Successfully logged in"); flag=0; break; } } st.close(); if(flag==1) System.out.println("Incorrect username or password"); } catch(IOException e) { e.printStackTrace(); } } else if(ch==3) break; } } }
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
Can you check my program if i make a inheritance. Because my teacher want us to use inheritance in jdoodle to run our program. I am still learning. Can you correct my program if its i don't use inheritance. Thanks.
import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) {
String uname,passw;
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("1.Registration");
System.out.println("2.Login");
System.out.println("3.Exit");
System.out.print("Choose an option: ");
int ch=sc.nextInt();
if(ch==1){
System.out.print("Enter your username: ");
uname=sc.next();
while(true){
if(uname.matches("[a-zA-Z0-9]+"))
break;
else{
System.out.println("Only alphanumerics are allowed");
System.out.print("Enter your username: ");
uname=sc.next();
}
}
System.out.print("Enter your password: ");
passw=sc.next();
while(true){
if(passw.matches("[a-zA-Z0-9]+"))
break;
else{
System.out.println("Only alphanumerics are allowed");
System.out.print("Enter your password: ");
passw=sc.next();
}
}
try {
BufferedWriter out = new BufferedWriter(
new FileWriter("records.txt", true));
out.write(uname+" "+passw);
out.close();
}
catch (IOException e) {
System.out.println("exception occurred" + e);
}
}
else if(ch==2){
System.out.print("Enter username: ");
uname=sc.next();
System.out.print("Enter password: ");
passw=sc.next();
try
{
FileInputStream fis=new FileInputStream("records.txt");
Scanner st=new Scanner(fis);
int flag=1;
while(st.hasNextLine())
{
String s=st.nextLine();
String[] d=s.split(" ");
if(d[0].equals(uname) && d[1].equals(passw)){
System.out.println("Successfully logged in");
flag=0;
break;
}
}
st.close();
if(flag==1)
System.out.println("Incorrect username or password");
}
catch(IOException e)
{
e.printStackTrace();
}
}
else if(ch==3)
break;
}
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY