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();
}
}
Related
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)
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");
}
}
}
I'm trying to convert some strings, I'd like to be able to remove diacritics from strinf. (Exemple : éùèà would become euea)
i have try this :
static str AALRemoveDiacritics( System.String input )
{
int i;
System.Text.NormalizationForm FormD;
str normalizedString = input.Normalize(FormD);
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
for (i = 0; i < strLen(normalizedString); i++)
{
System.Char c = normalizedString[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
{
stringBuilder.Append(c);
}
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
It looks like you tried making this post work in X++ and were very close.
Here's a working job I just wrote you can use:
static void AlexRemoveDiacritics(Args _args)
{
str strInput = 'ÁÂÃÄÅÇÈÉàáâãäåèéêëìíîïòóôõ£ALEX';
System.String input = strInput;
str retVal;
int i;
System.Char c;
System.Text.NormalizationForm FormD = System.Text.NormalizationForm::FormD;
str normalizedString = input.Normalize(FormD);
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
for (i = 0; i <= strLen(normalizedString); i++)
{
c = System.Char::Parse(subStr(normalizedString, i, 1));
if (System.Globalization.CharUnicodeInfo::GetUnicodeCategory(c) != System.Globalization.UnicodeCategory::NonSpacingMark)
{
stringBuilder.Append(c);
}
}
input = stringBuilder.ToString();
input = input.Normalize();
retVal = input;
info(strFmt("Before: '%1'", strInput));
info(strFmt("After: '%1'", retVal));
}
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
I have an assignment where I am required to read a file from a textfile and add the lines to a list, sort it by length and print it out. My problem is that Coolections.sort() sorts it both alphabetically and by length, I am required to only sort it by length, here is my code so far.
import java.util.*;
import java.io.*;
public class Question3
{
public static void main(String [] args) throws IOException
{
Scanner input = new Scanner(System.in);
String path = input.nextLine();
Scanner scanner = new Scanner(new File(path));
List<String> lines = new ArrayList<String>();
while(scanner.hasNext())
{
lines.add(scanner.next());
}
Collections.sort(lines);
System.out.println(lines);
}
}
Collections.sort(strings, new Comparator<String>() {
#Override
public int compare(String str1, String str2) {
if(str1.length() > str2.length())
return 1;
else if(str1.length() < str2.length())
return -1;
else
return 0;
}
});
This will sort the strings based on their length in ascending order, please clean up the above code as required.