Please check these code - if-statement

could you check these codes?
I can't find the problem.
"If" has no action!
It should check username and password and age.
After that if all of details are true will answer true unless will answer false.But "If" don't answer.
import java.util.Scanner;
public class Class2 {
public static void main(String[] args) {
int age;
String password = "big.110#go";
String username = "big";
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Please enter your Usename: ");
username = keyboardInput.next();
System.out.print("Please enter your age: ");
age = keyboardInput.nextInt();
System.out.print("Please enter your password: ");
password = keyboardInput.next();
if(keyboardInput.next().equals(username)
&& keyboardInput.nextInt() >= 18
&& keyboardInput.equals(password)) {
System.out.print("Welcome");
} else {
System.out.print("Something is wrong!\n Try again");
}
}
}

You made a lot of mistake in your code.
import java.util.Scanner;
public class Class2 {
public static void main(String args[]) {
int age;
String password1 = "big.110#go";
String username1 = "big";
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Please enter your Usename: ");
String username = keyboardInput.nextLine();
System.out.print("Please enter your password: ");
String password = keyboardInput.nextLine();
System.out.print("Please enter your age: ");
age = keyboardInput.nextInt();
if(username.equals(username1)
&& password.equals(password1)
&& age>=18 ) {
System.out.print("Welcome");
} else {
System.out.print("Something is wrong!\n Try again");
}
}
}

Related

if else in a for loop to check conditions inside an array

i am using splashkit library for c++ and have a change name function, ideally once the user inputs the values in the array names[i], then when using this we are trying to check if the input value already exists if it exists then select that value and replace it with the user input.
In case the user input does not match any of the array names[] then it should display a message like(Sorry try again or sorry this value doesn't match with any listed values).
my code is only working properly when i have two same array values then it recognises the input and let the user change the value to a new value. if i have 3 different values stored in array it is not checking the conditions and not working as intended.
void change_name(string names[], string name , int size)
{
int i;
i = 0;
//Declare variables
//your code(s) and your own comment here
//ask them to enter the name to change
//your code(s) and your own comment here *What data type?
//Check the name is in the array
//your code(s) and your own comment here *What data type?
// your code(s) and your own comment here
write_line("Which name would you like to change: ");
name = read_string(" Name: ");
// for (i = 0; i < SIZE; i++)
// {
if(name != names[i])
{
write_line("Sorry this option does not exist");
//names [i] = read_string(" Name: ");
}
else if (name == names[i])
{
write_line("Please enter a name: ");
names[i] = read_string(" Name: ");
}
//}
}
void change_name(string names[], string name , int size)
{
int i;
i= 0;
write_line("Which name would you like to change: ");
name = read_string(" Name: ");
for (i = 0; i < SIZE; i++)
{
if (name == names[i])
{
write_line("Please enter a name: ");
names[i] = read_string(" Name: ");
}
}
if(i==SIZE)
{
write_line("Sorry this option does not exist");
}
}

Function to do decryption

I need help with function string decryptedpassword() which is a part of a program to decrypt a password. I have to utilize const VOWELS to identify presence of vowels and insert 0 before and e after each vowel dynamically.
The main goal of program is to make an authentication system that begin with some initial arguments containing his or her credentials to be validated by the program.
These arguments are stated in the command line and should consist of two values, one for username and the other for password. Fortunately, the client has already specified that Kalle will use the following credentials:
Username: Kalle
Password: bAnanASplit
These arguments are passed to main(..) which in turn hands them over to mainArgumentsParser(..), a function devoted to the process of parsing out values. You are free to study how this is done but may not modify any of the code you find in these functions. The parser will return a string value containing user
credentials in the form username,password, or simply ”fail” to inform main(..) that too few arguments were provided.
The string of credentials are then passed to authenticateUser(..) which will need to separate the username from password and store them in separate strings. As can be seen in the function definition, there already exists two string constants.
const string USERNAME = "Kalle";
const string PASSWORD = "i0J0u0j0u0J0Zys0r0{";
#include "Prototypes.h"
using std::string;
using std::stringstream;
using std::cout;
using std::endl;
// If argument less than 3 so print fail.
string mainArgumentParser(int argc, char* argv[]) {
if (argc < 3)
return "fail";
std::stringstream ss;
ss << argv[1] << "," << argv[2];
return ss.str();
}
//bool function to separate username and password and validate them.
bool authenticateUser(string value)
{
const string USERNAME = "Kalle";
const string PASSWORD = "i0J0u0j0u0J0Zys0r0{";
bool authPassed = false;
string username = value.substr(0, 5);
size_t pos = value.find(",");
string pass = value.substr(pos + 1);
if (username == USERNAME && decryptPassword(pass) == PASSWORD )
{
authPassed = true;
}
return authPassed;
}
// bool to detremine if the number is even or not.
bool even(int x)
{
return x % 2 == 0;
}
//function to decrypt the paasword and return the result to validate the authentication.
string decryptPassword(string password)
{
const int ROT7 = 7, ROT9 = 9;
const string VOWELS = "AEIOUYaeiouy";
string decryptedpass;
for (size_t i = 0; i < password.length(); i++) {
if (even(i)) {
decryptedpass += password[i] + ROT7;
}
else {
decryptedpass += password[i] + ROT9;
}
decryptedpass += '0';
}
return decryptedpass;
}

"if" doesn't work with "keyboard.nextLine()"

I would have to write the correct password and if it is correct it say "password is correct"
When I write the correct password it say that it isn't correct.
Her is my code:
import java.util.Scanner;
class myclass
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
String name;
String password;
System.out.println("give your name:\t");
name=keyboard.nextLine();
System.out.println("Hello"+name);
System.out.println("give your password:\t");
password=keyboard.nextLine();
keyboard.close();
if (password=="www")
{
System.out.println();
System.out.println("password is correct") ;
}
else
{
System.out.println();
System.out.println("password isn't correct");
}
}
}
You have to use the method equals instead of == then it will work.
Looks like that:
import java.util.Scanner;
class myclass {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
String name;
String password;
System.out.println("give your name:\t");
name = keyboard.nextLine();
System.out.println("Hello" + name);
System.out.println("give your password:\t");
password = keyboard.nextLine();
keyboard.close();
if (password.equals( "www")) {
System.out.println();
System.out.println("password is correct");
} else {
System.out.println();
System.out.println("password isn't correct");
}
}
}
Output:
give your name:
name
Helloname
give your password:
www
password is correct
You should not compare String using == operator. You should use equals method to compare String object. e.g. "www".equals(password)

It's checking only firs row in table. help someone :) C# winForm

SqlConnection con = new SqlConnection(#"Data Source=(local)\SQLEXPRESS; Integrated Security= SSPI;" + "Initial Catalog = CarRent_vaxo;");
con.Open();
string strSQL = "select * from AccesList";
SqlCommand myCommand = new SqlCommand(strSQL, con);
using (SqlDataReader myDataReader = myCommand.ExecuteReader())
{
myDataReader.Read();
{
if (usertextBox.Text == myDataReader["User"].ToString() && paswordTextBox.Text == myDataReader["Password"].ToString())
{
this.Hide();
ResultForm rf = new ResultForm();
rf.Show();
}
else if (usertextBox.Text == "" || paswordTextBox.Text == "")
{
Notificarion2.Text = "PLEASE FILL THE BOXES !!!";
}
else
{
Notificarion2.Text = "WRONG USER OR PASSWORD !!!";
usertextBox.Text = "";
paswordTextBox.Text = "";
}
}
}
You should read from SqlDataReader in a while loop as
while (myDataReader.Read())
{
//your code goes here
}
myDataReader.Close();
http://msdn.microsoft.com/en-us/library/haa3afyz%28v=vs.110%29.aspx

Print Web Service Response

I am trying to call a webservice and print the some of the response.
When I run this code, I get XML response with ID, FIRSTNAME, LASTNAME, STREET, CITY. So for example how can I print out only CITY?
static int customerId = 123456;
public static void main(String[] args) throws Exception {
URL oracle = new URL(
"http://www.thomas-bayer.com/sqlrest/CUSTOMER/" + customerId);
BufferedReader in = new BufferedReader(new InputStreamReader(
oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
Thank you in advance.
This may be a tweak code but still this will do.
static int customerId = 123456;
static String str="";
public static void main(String[] args) throws Exception
{
URL oracle = new URL("http://www.thomas-bayer.com/sqlrest/CUSTOMER/" + customerId);
BufferedReader in = new BufferedReader(new InputStreamReader(
oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
//code change stats here
if(inputLine.contains("<CITY>")){
str=inputLine;
}
}
String city=str.replace("<CITY>","");
System.out.println(city.replace("</CITY>", ""));
//code change ends here
in.close();
}
This should be the best one :
Call this method in the while loop by passing the key and the string:
public static String getvalue(String xmlkey,String xmlstring) throws
ParserConfigurationException, SAXException, IOException{
System.out.println(xmlstring+"dff");
InputStream is = new ByteArrayInputStream(xmlstring.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = null;
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName(xmlkey);
if (nl != null) {
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
  String name = item.getNodeName();
  String value = item.getTextContent();
System.out.println(name+" "+value+" value and name");
}
}
return value;
} catch(Exception e) {
e.printStackTrace();
}
   
}