Returning New Lines - java

public String toString()
{
String name = "Customer name: " + this.name;
String address = "Customer address: " + street + " " + city + " " + state + " " + zip;
String bala = "Balance: " + bal;
return name + "\n" + address + "\n" + bala;
}
I want these variables to be returned in three separate lines, but the actual string "\n" is just being added where I want the new line to be. Any help?

Untested, but may work :
return name + System.lineSeparator() + address + System.lineSeparator() + bala;

Related

Finding exact string using line.contains

Apologies if some of this information comes across as lacking knowledge, just started learning Java.
In this working the user searches for both road and town. The problem being that when searching for something like 'Cabramatta' the result 'Cabramatta West' will also appear in the results.
The format of the information being read is as follows:
William Street^3^3503^Collins Street^Cabramatta West
William Street^3^3503^Collins Street^Cabramatta
while(fileName.hasNext())
{
String line =fileName.nextLine();
{
if(line.contains(suburbInput) && line.contains(roadInput))
{
String tramDetails[] = line.split("\\^");
String crossStreet = tramDetails[0];
String stopNumber = tramDetails[1];
int stopNumberInt = Integer.parseInt(stopNumber);
String trackerID = tramDetails[2];
int trackerIDInt = Integer.parseInt(trackerID);
String roadName = tramDetails[3];
String suburbName = tramDetails[4];
System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet + " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");
How do I go about getting it to just find results for 'Cabramatta' when it's searched but also find results for 'Cabramatta West' when that's searched?
You're going to have to split up your inputs before your check so you can use .equals instead of .contains.
while(fileName.hasNext())
{
String line =fileName.nextLine();
String tramDetails[] = line.split("\\^");
String suburbName = tramDetails[4];
String roadName = tramDetails[3];
if(suburbName.equals(suburbInput) && roadName.equals(roadInput))
{
String crossStreet = tramDetails[0];
String stopNumber = tramDetails[1];
int stopNumberInt = Integer.parseInt(stopNumber);
String trackerID = tramDetails[2];
int trackerIDInt = Integer.parseInt(trackerID);
System.out.print("'Suburb': " + suburbName
+ " 'Road': " + roadName
+ " 'Cross Street': " + crossStreet
+ " 'Stop': " + stopNumberInt
+ " 'Tracker ID': " + trackerIDInt + "\n");
}
Split before and then just use equals method of the String. Here is a test code:
String line = "William Street^3^3503^Collins Street^Cabramatta West";
String suburbInput = "Cabramatta";
String roadInput = "Collins Street";
String tramDetails[] = line.split("\\^");
String crossStreet = tramDetails[0];
String stopNumber = tramDetails[1];
int stopNumberInt = Integer.parseInt(stopNumber);
String trackerID = tramDetails[2];
int trackerIDInt = Integer.parseInt(trackerID);
String roadName = tramDetails[3];
String suburbName = tramDetails[4];
if (suburbInput.equals(suburbName) && roadInput.equals(roadName))
System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet
+ " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");
suburbInput = "Cabramatta West";
if (suburbInput.equals(suburbName) && roadInput.equals(roadName))
System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet
+ " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");
And output:
'Suburb': Cabramatta West 'Road': Collins Street 'Cross Street': William Street 'Stop': 3 'Tracker ID': 3503
Hope this helps!
You could uses String#endsWith() instead of String#contains() for the suburb:
if (line.endsWith(suburbInput) && line.contains(roadInput))
Of course, this is only a band-aid. 'Cabramatta' would still match 'West Cabramatta' The problem is the if (...) statement, as implemented, is only able to find probable matches. You need to parse the line into the exact fields you want to match against, and then test those fields explicitly.
Alternately (sledge hammer approach), you could implement a regular expression matcher that will match everything exactly in one go.

How to parse the returned string?

How to parse the returned string to get the key and value of each pair, and put it all in the collection?What kind of regex use?
#Override
public String toString() {
return "Продукт {" +
" Название продукта = '" + _productName + '\'' +
", Код продукта = '" + _productCode + '\'' +
", Марка металла = '" + _brandMetal + '\'' +
", Вес продукта = " + _weight +
'}';
}
like this:
#Override
public String toString() {
return "Название продукта = " + this.GetProductName() + ";" + " Код продукта = " + this.GetProductCode() + ";" + " Марка металла = " + this.GetBrandMetal() + ";" + " Вес продукта = " + this.GetWeightOfProduct() + ";";
}

Android logcat - not ouputing double new line - String with double new line - String equals

I'm testing a method in my app. I'm generating a vcard from a stored contact and comparing it to an hardcoded vcard. They should be the exact same vcard, and they are. The problem is the test is failing and don't understand how.
From the logcat output it looks obvious that it is not printing a double new line (\n\n)
Here is my code:
public void testGetContactAsVcard() {
String vcardActual = utils.getContactAsVcard(contact.getAllContactIds().get(0));
// This is the vcard for the first contact (position zero)
String vcardExpected = "BEGIN:VCARD" +
"\nVERSION:2.1" +
"\nN:Klisnmann;Jurgen;;;" +
"\nFN:Jurgen Klinsmann" +
"\nX-ANDROID-CUSTOM:vnd.android.cursor.item/nickname;mynickname;;;;;;;;;;;;;;" +
"\nTEL;CELL:922-222-222" +
"\nTEL;WORK:911-222-333" +
"\nEMAIL;WORK:work#mail.com" +
"\nEMAIL;HOME:home#mail.com" +
"\nADR;WORK:poBox;;StreetSomething;Berlin Alexanderplatz;Berlin;28000;Germany" +
"\nADR;HOME:poBox;;MyStreet20;Berlin Potsdam;Berlin;2800;Germany" +
"\nORG:MAT\\;MobileDept." +
"\nTITLE:Eng." +
"\nURL:www.company.com" +
"\nURL:myblog.com" +
"\nURL:custom.com" +
"\nPHOTO;ENCODING=BASE64;JPEG:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwM" +
"\n EAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/" +
"\n 2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUF" +
"\n BQUFBQUFBQUFBQUFBT/wAARCAAkAGADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAA" +
"\n ECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaE" +
"\n II0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZn" +
"\n aGlqc3R1d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itr4R1K9tBevcHyEkuPIQzSeSyLIkhIRc73LozZ6X" +
"\n wj4bddRl1h9c8XXVrLI11ZaLq7rHFa5EisRhFkYSeYXEVzI/lnYFSHYFXh4vBPipXiaTwvBNs" +
"\n GCrXaKH4wM7ZB7cjGcc55zWPw98Wkk/2Fj2F5F/8VWvsanLZSX3r/P+vvvze0p31T+5/wCR6L" +
"\n 4Z+KFxrtnc3l74H8V+HbS3muoHk1OzhZy0EkceUhgmlldZGeTy2VCrLbu+QjwtLpeBvGl3400" +
"\n rT9Qm8J674ZhvbCK+WLXUt4poi7OPIkijmd0lVVVmUjAEijO8OqeUf8K88W/9AP8A8nIf/iq2" +
"\n oqCwooooAKKKKACiiigD/2Q==" +
"\n\nNOTE:Somenotes..." +
"\nX-ANDROID-CUSTOM:vnd.android.cursor.item/contact_event;1985-07-20;1;;;;;;;;;;;;;" +
"\nEND:VCARD";
Log.d(TAG, "vcard actual: " + vcardActual);
Log.d(TAG, "vcard expected: " + vcardExpected);
assertEquals(vcardExpected, vcardActual);
}
And here is the logcat output:
vcard actual:
BEGIN:VCARD
VERSION:2.1
N:Klisnmann;Jurgen;;;
FN:Jurgen Klinsmann
X-ANDROID-CUSTOM:vnd.android.cursor.item/nickname;mynickname;;;;;;;;;;;;;;
TEL;CELL:922-222-222
TEL;WORK:911-222-333
EMAIL;WORK:work#mail.com
EMAIL;HOME:home#mail.com
ADR;WORK:poBox;;StreetSomething;Berlin Alexanderplatz;Berlin;28000;Germany
ADR;HOME:poBox;;MyStreet20;Berlin Potsdam;Berlin;2800;Germany
ORG:MAT\;MobileDept.
TITLE:Eng.
URL:www.company.com
URL:myblog.com
URL:custom.com
PHOTO;ENCODING=BASE64;JPEG:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwM
EAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/
2wBDAQMEBAUEBQkFBQkUDQsNFBQ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VSd4NO/l/w34L7zWU3Pf+vvuekUUUVqQF
cj8XoI7j4VeMVljWVRpF24V1BAZYXKn6ggEehArrqz/EOnyatoGp2MLIs1zaywoXJChmQgZxn
jmgDy34u/C3TfHzaBd3vjD4k+HbLR/tFitr4R1K9tBevcHyEkuPIQzSeSyLIkhIRc73LozZ6X
wj4bddRl1h9c8XXVrLI11ZaLq7rHFa5EisRhFkYSeYXEVzI/lnYFSHYFXh4vBPipXiaTwvBNs
GCrXaKH4wM7ZB7cjGcc55zWPw98Wkk/2Fj2F5F/8VWvsanLZSX3r/P+vvvze0p31T+5/wCR6L
4Z+KFxrtnc3l74H8V+HbS3muoHk1OzhZy0EkceUhgmlldZGeTy2VCrLbu+QjwtLpeBvGl3400
rT9Qm8J674ZhvbCK+WLXUt4poi7OPIkijmd0lVVVmUjAEijO8OqeUf8K88W/9AP8A8nIf/iq2
oqCwooooAKKKKACiiigD/2Q==
NOTE:Somenotes...
X-ANDROID-CUSTOM:vnd.android.cursor.item/contact_event;1985-07-20;1;;;;;;;;;;;;;
END:VCARD
vcard expected:
BEGIN:VCARD
VERSION:2.1
N:Klisnmann;Jurgen;;;
FN:Jurgen Klinsmann
X-ANDROID-CUSTOM:vnd.android.cursor.item/nickname;mynickname;;;;;;;;;;;;;;
TEL;CELL:922-222-222
TEL;WORK:911-222-333
EMAIL;WORK:work#mail.com
EMAIL;HOME:home#mail.com
ADR;WORK:poBox;;StreetSomething;Berlin Alexanderplatz;Berlin;28000;Germany
ADR;HOME:poBox;;MyStreet20;Berlin Potsdam;Berlin;2800;Germany
ORG:MAT\;MobileDept.
TITLE:Eng.
URL:www.company.com
URL:myblog.com
URL:custom.com
PHOTO;ENCODING=BASE64;JPEG:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwM
EAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/
2wBDAQMEBAUEBQkFBQkUDQsN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4540VSd4NO/l/w34L7zWU3Pf+vvuekUUUVqQF
cj8XoI7j4VeMVljWVRpF24V1BAZYXKn6ggEehArrqz/EOnyatoGp2MLIs1zaywoXJChmQgZxn
jmgDy34u/C3TfHzaBd3vjD4k+HbLR/tFitr4R1K9tBevcHyEkuPIQzSeSyLIkhIRc73LozZ6X
wj4bddRl1h9c8XXVrLI11ZaLq7rHFa5EisRhFkYSeYXEVzI/lnYFSHYFXh4vBPipXiaTwvBNs
GCrXaKH4wM7ZB7cjGcc55zWPw98Wkk/2Fj2F5F/8VWvsanLZSX3r/P+vvvze0p31T+5/wCR6L
4Z+KFxrtnc3l74H8V+HbS3muoHk1OzhZy0EkceUhgmlldZGeTy2VCrLbu+QjwtLpeBvGl3400
rT9Qm8J674ZhvbCK+WLXUt4poi7OPIkijmd0lVVVmUjAEijO8OqeUf8K88W/9AP8A8nIf/iq2
oqCwooooAKKKKACiiigD/2Q==
NOTE:Somenotes...
X-ANDROID-CUSTOM:vnd.android.cursor.item/contact_event;1985-07-20;1;;;;;;;;;;;;;
END:VCARD
As you can see, in the vcardActual between the PHOTO and the NOTE there is a new empty line but that does not happen in the vcardExpected even tho I've placed a \n\n before the NOTE.
I would expect an empty new line between PHOTO and NOTE but for some reason it does not happen. Why and how is this happening?
Got the solution.
First -> Never rely solely on logcat! The new line was ALWAYS there but logcat wasn't outputting it for some reason that I don't know yet.
Second -> JUnit was adding a [, ], <, > and a new line.
Solution: Remove all whitespaces and new lines from both strings and compare them.
final String vcardNoWhiteSpacesNoNewLinesExpected = vcardExpected.replace("\n", "").replace("\r", "").replace(" ", "");
final String vcardNoWhiteSpacesNoNewLinesActual = vcardActual.replace("\n", "").replace("\r", "").replace(" ", "");
They both match now, problem solved.

Writing the resultset to csv file

I have a method getstaffinfo, which has 3 parameter (var_1, connection, filewriter fw), the var_1 value is read from a text file. So the method will be called as many times based on all the var_1 value passed from text file . approx ( 15000)
public static String getstaffid(String var_1, Connection connection,
FileWriter fw) throws SQLException, Exception
// Create a statement
{
String record = null;
ResultSet rs = null;
Statement stmt = connection.createStatement();
boolean empty = true;
try {
rs = stmt
.executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList<String> records = new ArrayList<String>();
while (rs.next()) {
empty = false;
//record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
for (int i = 1; i <= columns; i++) {
String value = rs.getString(i);
records.add(value);
}
for (int j = 0; j < records.size(); j++) {
record = records.get(j) + ",";
}
fw.append(record);
}
/*fw.append(rs.getString(1));
fw.append(',');
fw.append(rs.getString(2));
fw.append(',');
fw.append(rs.getString(3));
fw.append('\n'); */
} finally {
fw.flush();
rs.close();
stmt.close();
}
return record;
}
As you can see, am executing a query for 47 values, which could be null or it can have some value.
Then i iterate through this 47 column, take the value and store it to an array list. Then i iterate the array list and write all the values to the string record with comma seperated value. Which is written to a csv file.
But it does not work fine. Any inputs would be appreciated...
You may have already solved the problem. Just let you know that I tried to use your code just now and found the issue was here:
record = records.get(j) + ",";
You should use something like this:
record = record + records.get(j) + ",";
Also change String to StringBuffer will improve the performance.
You didn't write the exact problem you face, but there is one for sure: you never write a line break into the file, so all data gets in one line.
while (rs.next()) {
... // your code, with the for loops
fw.append(record); //writing out the line, from your code
fw.append("\r\n"); //line break -- add this line
} //this is the end of the "while(rs.next())" loop
...

java keeps changing the first zip code

I dont get why when i compile this code i get the incorrect zip code.
John Smith
486 test St.
Yahoo, MA 898 - 2597JohnSmith
486 test St.
Yahoo, MA 898 2597
Code
public class test
{
public static void main(String[] args) {
String firstName = "John";
String lastName = "Smith";
int streetNumber = 486;
String streetName = "test St.";
String city = "Yahoo";
String state = "MA";
int zip = 01602;
int zipplus4 = 2597;
System.out.print(firstName + " " + lastName + "\n" + streetNumber + " " + streetName + "\n" + city + ", " + state + " " + zip + " - " + zipplus4);
System.out.println(firstName + lastName);
System.out.println(streetNumber + " " + streetName);
System.out.println(city + ", " + state + " " + zip + " - " + zipplus4);
}
}
When you specify a number with a leading zero, it gets treated as an Octal (base-8, as opposed to decimal base-10 or hexadecimal base-16).
01602 octal == 898 decimal
Since Java wasn't desgined with Zip codes in mind, to get the desired effect, drop the leading zero, and format it when you print it:
System.out.println(city + ", " + state + " " + new java.text.NumberFormat("00000").format(zip) + " - " + new java.text.NumberFormat("0000").format(zipplus4));
Make those zip codes String instead of int and it'll be fine.
public class test
{
public static void main(String[] args)
{
String firstName = "John";
String lastName = "Smith";
int streetNumber = 486;
String streetName = "test St.";
String city = "Yahoo";
String state = "MA";
String zip = "01602";
String zipplus4 = "2597";
System.out.print(firstName + " " + lastName + "\n" + streetNumber + " " + streetName + "\n" + city + ", " + state + " " + zip + " - " + zipplus4);
System.out.println(firstName + lastName);
System.out.println(streetNumber + " " + streetName);
System.out.println(city + ", " + state + " " + zip + " - " + zipplus4);
}
}
Outcome:
John Smith
486 test St.
Yahoo, MA 01602 - 2597JohnSmith
486 test St.
Yahoo, MA 01602 - 2597
Process finished with exit code 0
I'd also advise you to encapsulate those into sensible objects. Why deal with String primitives when you can use an Address class? Java's object-oriented; better to think in terms of objects.
01602 - this 0 at the beginning means you are using octal rather than decimal numbers. Remove it and you'll be fine :-).
BTW IntelliJ IDEA even displays warning here.
You should use String type for zip and zipplus4.
If you cannot change the type then you can use the following in your println statement
String.format("%05d", zip)
Take off the leading zero~ or make it a string
A Zipcode shouldn't be stored in a numeric datatype because it isn't really something you wnat to do math on, instead store it as a String and it'll work fine.

Categories