ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Plz help me in writing Java equivalent code for this problem!

(1/1)

simsim00:
This is my C++ code for the problem given below. But since i'm newbie to java, dont know how to write equivalent Java code. If you people can help, plz do so. Helping hands are always praised worthy. Im stuck...Plz assist me..

Code for Login page in C++
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
string readString(ifstream&);
int main(int argc, char *argv[])
{
char user[80];
char pass[80];
string line_in;
string::size_type pos;
ifstream fp_in;
int count = 0;
string user_pass[100][2];
fp_in.open("users.txt");
if(fp_in == NULL)
{
cout << "Could not open user file, exiting." << endl;
return -1;
}
while(!fp_in.eof())
{
line_in=readString(fp_in);
if(line_in.length() > 0)
{
pos = line_in.find("\t",0);
if( pos != string::npos)
{
user_pass[count][0] = line_in.substr(0,pos);
user_pass[count][1] = line_in.substr(pos+1);
count++;
}
}
}
fp_in.close();
if(!count > 0)
{
cout << "Userlist is empty, exiting" << endl;
return -2;
}
int logged_in=0;
while(!logged_in)
{
cout << "Enter user name: ";
cin >> user;
cout << "Enter password: ";
cin >> pass;
int found = 0;
for(int x=0;x<count && !found;x++)
{
if(user_pass[x][0].compare(user) == 0)
{
found = 1;
if(user_pass[x][1].compare(pass) == 0)
{
found = 1;
logged_in = 1;
}
else
{
cout << "Invalid password!" << endl;
}
}
}
if(!found)
{
cout << "Invalid username!" << endl;
}
}
cout << "Logged in successfully!" << endl;
return 0;
}
string readString(ifstream& reader)
{
char buffer[256];
reader.getline(buffer,256);
string str1 = buffer;
return str1;
}

Problem:
Writing a maintainable code makes the product with High Quality. Here you are given a scenario where you have to write a maintainable code for the given problem.

Scenario:


Virtual Solutions is an IT solution provider company. It has hired you as a Software Engineer. Your job is to write code, but at the same it should be of High Quality. Your Team Lead has asked you to write a very small code of user login functionality. The code should be of High Quality.

You are required to

a. Write correct code for the user login in Java following the guidelines for writing code
b. Make use of Hungarian Notation for coding purpose

c. Make use of Comments in the Code where required necessary.

javafreak:
   import java.util.*;
   import java.io.*;
   public class login
   {
   public static void main(String ar[])
   {   
   try
   {
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
   String s;Integer pass;
   System.out.println("enter the Username");
   s=input.readLine();
   System.out.println("enter the password must be numeric");
   pass=Integer.parseInt(input.readLine());
   if(s.equals("sam") && pass.equals(123) )
   {
   System.out.println("you are logined sucess fully");
   }
   else
   {
   System.out.println("you have entered the wrong");
   }
   }
      catch (Exception e) {
        e.printStackTrace();
      }
   }
   }

Navigation

[0] Message Index

Go to full version