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.
Related
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.
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;
I'm new to programming and logical errors; I've looked for a similar problem but nothing quite matches mine. We have an assignment to make a program where the user enters their first and last name--if the names are the same, it returns "your first and last name are the same" and if the names are different, it returns "your first and last name are different".
My problem is that it keeps returning BOTH answers no matter what names are typed in. If you run it and try your own name in it, you'll see what I mean.
Here's my code (we have to use scanner):
import java.util.Scanner;
public class NameAssignment
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String firstName;
System.out.print("Enter your first name: ");
firstName = stdIn.nextLine();
String lastName;
System.out.print("Enter your last name: ");
lastName = stdIn.nextLine();
if (firstName == lastName);
System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are the same.");
if (firstName != lastName);
System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
} // end main
} // end class NameAssignment
UPDATE
I tried correcting it with the suggestions below, and here's what it looks like now:
import java.util.Scanner;
public class NameAssignment
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String firstName;
System.out.print("Enter your first name: ");
firstName = stdIn.nextLine();
String lastName;
System.out.print("Enter your last name: ");
lastName = stdIn.nextLine();
if (firstName.equals(lastName))
System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are the same.");
else (!firstName.equals(lastName)
System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
} // end main
} // end class NameAssignment
I ran it in terminal, and it gave me this:
NameAssignment.java:16: error: ')' expected
else (!firstName.equals(lastName)
^
NameAssignment.java:16: error: not a statement
else (!firstName.equals(lastName)
^
NameAssignment.java:17: error: illegal start of expression
System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
^
NameAssignment.java:17: error: ';' expected
System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
^
4 errors
To compare 2 Strings in java, you must use if (firstName.equals(lastName)) because in Java == operator checks 2 String objects and .equals() checks String values.
Also, you must remove the semicolon ; after the if statement or else that wont execute your if condition.
Your else clause is wrong. Just replace your else clause with the following else clause:
else
System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
That's it. else doesn't need a condition. That's why else (!firstName.equals(lastName)) is wrong.
you were missing the ) which i added and removed the "," + " " because it was un-efficient and it should just be included in the same string which i fixed. Just replace your code with that and it should work
if (firstName.equals(lastName)){
System.out.println("Hello " + firstName + " " + lastName + ", your first name and last name are the same.");
}
else (!firstName.equals(lastName)){
System.out.println("Hello " + firstName + " " + lastName + ", your first name and last name are different.");
}
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 aGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpK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LozZ6X" +
"\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/
2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQ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
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/
2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU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qQF
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.
I'm coding a simulation of a sports game, and it works fine for the most part; compiles and runs like it should. The directions ask that I I assume that I am supposed to be using printf and %.2f, but whenever I try to incorporate that into my code, it ceases to run properly. Help would be much appreciated!
import java.util.Scanner;
public class Team {
public String name;
public String location;
public double offense;
public double defense;
public Team winner;
public Team(String name, String location) {
this.name = name;
this.location = location;
this.offense = luck();
this.defense = luck();
}
public double luck() {
return Math.random();
}
Team play(Team visitor) {
Team winner;
double home;
double away;
home = (this.offense + this.defense + 0.2) * this.luck();
away = (visitor.offense + visitor.defense) * visitor.luck();
if (home > away)
winner = this;
else if (home < away)
winner = visitor;
else
winner = this;
return winner;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter name and location for home team (on separate lines)");
String homeName = s.next();
String homeLocation = s.next();
Team homeTeam = new Team(homeName, homeLocation);
System.out.println("Enter name and location for home team (on separate lines)");
String awayName = s.next();
String awayLocation = s.next();
Team awayTeam = new Team(awayName, awayLocation);
Team winnerTeam = homeTeam.play(awayTeam);
System.out.printf("Home team is:" + homeName + " from" + homeLocation + " rated" + homeTeam.offense + " (offense) +" + homeTeam.defense + " (defense)" + "\n");
System.out.printf("Away team is:" + awayName + " from" + awayLocation + " rated" + awayTeam.offense + " (offense) +" + awayTeam.defense + " (defense)" + "\n");
System.out.printf("Winner is:" + winnerTeam.name + " from" + winnerTeam.location + " rated" + winnerTeam.offense + " (offense) +" + winnerTeam.defense + " (defense)" + "\n");
}
You have misunderstood the printf method. You do not concatenate strings the way you do in this line and its successors (reformatted for width reasons):
System.out.printf("Home team is:" + homeName +
" from" + homeLocation +
" rated" + homeTeam.offense +
" (offense) +" + homeTeam.defense +
" (defense)" + "\n");
This is like the way an old coworker tried to use PreparedStatements to prevent SQL injection attacks, but constructed the query string by concatenation anyway, making the attempt ineffective. Instead, look at the signature of printf:
public PrintWriter format(String format, Object... args)
The first argument is a format string, which contains static text and format directives beginning with %. In typical use, each format directive corresponds to one argument of the method. Replace the interpolated variables with directives.
Strings are usually formatted with %s: s for string. Doubles are usually formatted with %f: f for float (or double). Characters between the % and the letter are options. So, let's replace the strings you interpolated with directives:
"Home team is: " + "%s" + // Inserted a space.
" from" + "%s" +
" rated" + "%6.2f" + // Six characters, 2 after the decimal.
" (offense) +" + "%6.2f" +
" (defense)" + "%n" // %n means the appropriate way to get a new line
// for the encoding.
Now we put it all together:
System.out.format("Home team is: %s from %s rated %6.2f (offense) + %6.2f (defense)%n",
homeName, homeLocation, homeTeam.offense, homeTeam.defense);
This is a lot simpler. Additionally, another reason to avoid interpolating strings in a format string is that the strings you interpolate may contain a percent sign itself. See what happens if you unguardedly write this:
String salesTax = "5%";
System.out.format("The sales tax is " + salesTax);
That's equivalent to
System.out.format("The sales tax is 5%");
Unfortunately, the percent sign is treated as a format directive, and the format statement throws an exception. Correct is either:
System.out.format("The sales tax is 5%%");
or
String salesTax = "5%";
System.out.format("The sales tax is %s", salesTax);
But now I should ask why you did not take homeName and homeLocation from Team. Certainly they are more relevant to Team than to each other. In fact, you should look up the Formattable interface, and with proper coding you can write:
System.out.format("%s%, homeTeam);
Try this:
public class A {
public static void main(String[] args) {
System.out.println(String.format("%.2f", 12.34123123));
}
}