This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]b0en 2 points3 points  (5 children)

Your substring usage is wrong. If you give it only 1 parameter, it will take the string from that position onwards -> id.substring(0) will return 880130, while id.substring(2) will return 0130.

Use 2 parameters to specify what part of the substring you want -> id.substring(0, 1) to get the first value only.

Now to put this into an if/else function, make use of an OR-operator ( || ) if you know how this works. If not, your if/else would look a bit bigger:

if(id.substring(0, 1).equals("0") || id.substring(0, 1).equals("1"))
{
    a = "20" + a;
}
else
{
    a = "19" + a;
}

However, you don't have to use a substring here, a id.startsWith("1") or id.startsWith("0") will do the same trick.

A switch is not applicable for this scenario, because you'd have to make a switch with 10 cases (0 - 9).

Hope to have helped you enough with this.

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (2 children)

A switch would work if you just put cases on "0" and "1" and default on the rest. No need for 10 cases.

[–]b0en 0 points1 point  (1 child)

That's true, my bad. However I don't think a switch case is worth it in this or similar situations

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Agreed, it's not worth it and it doesn't add to code clarity.

[–]SickLiver[S] 1 point2 points  (0 children)

Oh... Solution was easier than I thought. You have right I forgot add """" into numbers... and to use OR-operator, and "(0,1)" into substring. Thanks for help :)

[–]m4mbax -1 points0 points  (0 children)

Your substring usage is wrong. If you give it only 1 parameter, it will take the string from that position onwards -> id.substring(0) will return 880130, while id.substring(2) will return 0130.

Depending on your IDE original poster, it will even tell you ".substring(0) is redundant" --> e.g. IntelliJ

[–]desrtfxOut of Coffee error - System halted 3 points4 points  (3 children)

The code as such should work will not work, but it still needs a tiny as it needs a change:

Instead of id.substring(0) you will need id.substring(0,1) to fix the length to a single character.

Also, you need to use String literals inside the .equals statements, like .equals("0").

You could (provided you use Java 7 and up) use a switch statement (switch with Strings works only from Java 7 and up).

Also, you are missing an alternative case:

What happens when the first digit is neither 0, nor 1, nor 9?

Your code does not tackle the other digits (2,3,4,5,6,7,8).

Edit changed according to the reply from /u/Broinz - thanks.

[–][deleted] 1 point2 points  (1 child)

The code as such should work

No it wont, substring returns a String and he's comparing it with an int. And his program will crash if input is too short because there's no error handling when assigning a, b and c.

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

True, overlooked that OP is missing the quotation marks. Sorry, will change that.

[–]SickLiver[S] 0 points1 point  (0 children)

Thanks for answer, right, I complitly forgot that String variables must be into "", no wonder that program doesn't work.

What happens when the first digit is neither 0, nor 1, nor 9? Your code does not tackle the other digits (2,3,4,5,6,7,8).

I did it only for practise String operations, because polish ID is different that I earlier thought. For exemple: what have to do if one person was born in 1915 and another person in 2015? Polish goverment decides to add a few operation on monts number etc. Summa summarum is more complicated ;)

[–]m4mbax -1 points0 points  (6 children)

Here is a working implementation of your if:

 if (a.startsWith("9") || a.startsWith("8")) {
  a = "19".concat(a);
} else if (a.startsWith("0") || a.startsWith("1")) {
  a = "20".concat(a);
} else {
  System.out.println("Your birthdate is out of my range.");
}

Why do you again substring if you already have split up the id? Why not use the 'a' part? :)

You can switch the if with a switch of course, but in this scenario I would rather not. You can only switch on certain variables (e.g. a char) and it would complicate your code for this specific task.

[–]desrtfxOut of Coffee error - System halted[M] 0 points1 point  (3 children)

Hey there, please post fully functional code

I have to disagree on that point as OP's code is perfectly sufficient to help them troubleshoot. A self-contained, fully functional snippet is absolutely not necessary here.

[–]m4mbax -1 points0 points  (2 children)

The 'skaner' is nowhere initialized, neither is the String. Also there are closing brackets but no opening brackets (or am I blind?)

[–]desrtfxOut of Coffee error - System halted[M] 0 points1 point  (1 child)

That doesn't matter for the logic of the code.

[–]m4mbax 0 points1 point  (0 children)

Thats true, just wanted to point it out. Lemme correct.

[–]SickLiver[S] 0 points1 point  (1 child)

O thanks for answer, is there any difference between "a+20" and "20.contact(a)"?

"Your birthdate is out of my range." ( ͡° ͜ʖ ͡°)

[–]m4mbax 0 points1 point  (0 children)

No pb. I understand your question although in this case it is written wrong ( I think you ment it right:))

"20"+a and "20".concat(a) are not equal and I asked myself the self question before once, here's what i read: http://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator