• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can anyone help me with this difficult ex:

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everyone I have an exercise to do that consists of:
-Develop a simple application with the following parameters
-countryCode (supported countries are en-US and de-DE)
-phoneNumber with text (e.g. 800CALLME1)
- The application must convert the given phone number (with any text) to the local US or German format without text.
example: 800CALLME1 returns (800) 225-5631 for the en-US country

basically i have done for the US as shown below but my problems are:
1- i don't know how to let the user make a US DE choice
2- i don't know how to match a DE and return a DE format number
ok now i will show what i have done until now and if someone cal help me thank you
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us what problems you are having. What do you expect to see and what do you actually see?
You can ask the user to input “DE” or “US” and use that as a case after switch.
You don't need more than one Scanner reading System.in.
Phone numbers are not numbers; use the definition of a number as something you do arithmetic with. Don't use a long to return phone numbers, least of all when phone numbers often start with 0.
The last thing you want is a method that long, least of all the main method. The ideal length for a main method is one statement; you can see an example here. And yes, that main method does contain one statement; its line 3 is not a statement but a declaration. All the “real” code should be in separate methods.
Are you being taught object‑oriented programming? That is a very non‑object‑oriented solution. I think it would require a TelephoneNumberConverter class with subtypes for US and Germany.
You can use chars as indices in an array; remember the char datatype is not a letter, but a number. You can also use 'B' − 'A' or 'B' % 0x20 as array indices. That might give you more elegant code than that switch.
If you use a switch with fall‑through, make sure to write a comment that the fall‑through is intentional. Also don't try to cram so much code into one line.
 
Jeton Shabani
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help.

I have now implemented my code no longer on the main and tried in my own way to make the user put DE or US,
I know I'm not very good at this, it's one of my first exercises in Java and I don't have much experience.
But at least works,
i only don't understand what type to use if i don't use long.
Here is my final code


and this is Launch

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeton Shabani wrote:Thank you . . . .

That's a pleasure

I have now implemented my code no longer on the main . . .

But you still have a vey long method. Why are you passing the args array to run()?


. . . one of my first exercises in Java . . .

Who is teaching you?

. . . i only don't understand what type to use if i don't use long.

The ideal would be a PhoneNumber class. You can create GemanPhoneNumber and USPhoneNumber subtypes. You could try String, but as you will see here, that is not what Strings are designed for.
Why did you use an index out of bounds exception? It is usually bad to throw an exception and catch it in the same place.
Your switch involves a lot of repeated code. It sounds like something that should be changed to a method call.
 
Jeton Shabani
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But you still have a vey long method. Why are you passing the args array to run()?


my error on reading the post to how not to work with in the main method...

Who is teaching you?


nobody... actually is a coding challenge on work (i work with CMS and JavaScript but now i want to start learning java) to see if i can manage to understand what i've learned on book, i've seen some tutorials and readed one book for Java, but nothing more.  I come from JavaScript and i want to learn Java now.

Why did you use an index out of bounds exception? It is usually bad to throw an exception and catch it in the same place.


i know but one of the exercises was: -Provide simple error handling
and i didn't have much time to make it better so just to show that can manage an exeption.

I know i have a lot to learn, but i need to start from somewhere..
Thank you again for responding me so quick.


 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcom!

Campbell Ritchie wrote:
Phone numbers are not numbers; use the definition of a number as something you do arithmetic with. Don't use a long to return phone numbers, least of all when phone numbers often start with 0.



There are also phone numbers and Phone Numbers.

A complete US phone number is something like +01 (000) 555-1212. The "01" is the US Country code. DE is, I think +44. Except for 1-800 numbers and the like, US phone numbers published internal to the USA usually omit the country code. Area code traditionally has been the same as that of the call originator if omitted, but in recent years most major metro areas have multiple overlapping area codes, so 10-digit numbers are now quite common.

It's even more fun in Britain, where they commonly have 2 different formats for phone numbers, often posted in the same neighborhood!

When parsing phone numbers, I recommend simply removing all "+", "(", ")", " ", "-" and "." from the text to normalise the format. Those are simply readability characters and you won't find them on a standard phone dial/keypad. Just for the record, though I absolutely DESPISE webapps that require the user to type in stuff like that as one long number without readability characters. It takes minimal extra coding to have the server do the removal and it's a lot more friendly to the user.

Plus, since Elon Musk believes that more lines of code == better programmer, you can boost your professional credibility.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:When parsing phone numbers, I recommend simply removing all "+", "(", ")", " ", "-" and "." from the text to normalise the format. Those are simply readability characters and you won't find them on a standard phone dial/keypad.


Be careful. Most of the characters you mentioned are just there for formatting, but '+' most definitely is not.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Tim Holloway wrote:When parsing phone numbers, I recommend simply removing all "+", "(", ")", " ", "-" and "." from the text to normalise the format. Those are simply readability characters and you won't find them on a standard phone dial/keypad.


Be careful. Most of the characters you mentioned are just there for formatting, but '+' most definitely is not.

I don't have a '+' on my phone (US).
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use a String for your 'number', not a long.

 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Be careful. Most of the characters you mentioned are just there for formatting, but '+' most definitely is not.


The plus prefix indicates that the number is formatted according to the E.164 standard.

In most places in the world, a prefix of 00 indicates that the remaining digits follow are an international numbering plan (same as the + with E.164), and prefix of 0 indicates that the remaining digits follows a national numbering plan (usually an area/city code plus the local number).  Without either prefix, the digits follows a local or network-specific numbering plan.

In North America however, we use 011 as the prefix for international and 1 for national.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and you can't deduce this from the organization of the digits?
 
Rancher
Posts: 326
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:DE is, I think +44


Close, but not quite. +44 is UK - germany is +49.

Back to topic:

I don't know much about the north american number(ing?) plan (nanp) - but as a german working in customer service for the past decade and thanks to a lot of current and historical information is well documented on wikipedia and other sources I can give you quite some insight in it:

First of all: In germany and I think in most of europe using letters for masking phone numbers is very uncommon. I can't even remember the last time I've seen it and IIRC the full number was written right next to it anyway. I'm not quite sure why from all of europe germany was chose as the example. We don't use it.
What you see way more often is replacing the area code with a short of the town name or, if it has a matching car license plate short, with it. I live in Magdeburg which, as the capital of Sachsen-Anhalt, is its own area. Our local car license plate code is MD. Our phone area code is 0391. So what you often see is like: "MD / 000000" (I used 0s as it's not a valid phone number). Which effective means: "0391 / 000000". That's way more common and works in a lot of areas with big towns as the towns themselfs often have a license plate short which matches the boundaries wherein the phone numbers are organized.

Next - Length of numbers: area codes can be as short as two-/three-digits (like berlin: (0)30) and up to five-/six-digit like (0)39203 (Barleben, a small village right next north to Magdeburg - about as old but refused to join ever since). The leading zero is only required if you don't use international format like +49. So, using +49-0-xxx doesn't work - the 0 has to be omitted.
After you figured out the area code then there's the actual local number. I'm not sure about the minimum - the shortest I've seen is only 4 digits - but they can get quite long. A regular local number is about 7 or 8 digits but can be longer. Mobile is limited to either 7 or 8. They can't be shorter but also can't be longer (at least I haven't seen them any longer than 8 digits after the provider code).

Third: Special sections: For non-mobile numbers they all start with 02x - 09x. 01x is reserved for mobile and payed services. There're however several blocks within the regular scheme: 032xxx - personalized numbers, 0800xxx toll free, 0900xxx payed services (up to several euros per minute) and a few others. There're also numbers starting with 1 - like 110/112 - emergency. Mobile is 015x - 017x but only certain blocks. 019x isn't used anymore but was moved over to 0900.
Up to early 2000s for mobile one could determine the carrier - but since this time port over is supported so you can switch carrier and even the network (currently there're 4 in operation) and can'T rely on the orginal provider codes anymore.

You see: This gets complicated rather quickly. As said: Wikipedia offers great knowledge of current and historical number plans. But I'm not sure if they're availble in other languages, too.
TLDR: At least in germany it's very uncommon to see something like 0800-abcdefg - we're just not used to it and most wouldn't understand to look on the keys on their phone and look at the letters on them. And if someone uses it the regular digits are usually right next to it.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . I don't have a '+' on my phone (US).

Nor have I, but Ron has explained what the + means.

Jeton Shabani wrote:. . . one of the exercises was: -Provide simple error handling . . .

That probably isn't what they mean by error handling. It probably means to provide some response if an incorrectly‑formatted number is supplied. That may entail throwing an exception.
I am not at all convinced that anybody can learn programming from a book. And I am not convinced that most online tutorials are any good.
 
Jeton Shabani
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That probably isn't what they mean by error handling. It probably means to provide some response if an incorrectly‑formatted number is supplied. That may entail throwing an exception.


so was I wrong to put in a try catch and do an IndexOutOfBoundsException ? what should I have done ?

I am not at all convinced that anybody can learn programming from a book. And I am not convinced that most online tutorials are any good.


to be honest neither am I.
at work they asked me to pass an exam (OCA) that I'm discovering now and I have 3 months to do it... I don't feel I can pass it because I have zero experience in Java and so they gave me exercises to keep me trained and discover Java, the problem is that with books I can't put myself in a position to understand, so I'm trying with the exercises.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeton Shabani wrote:. . . they asked me to pass an exam (OCA) that I'm discovering now . . .

Which OCA exam?
Good grief! Without proper training, you will find a cert exam very difficult. I am not at all happy to hear about your situation
 
Ron McLeod
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I don't have a '+' on my phone (US).


I do - this is my Canadian mobile phone - long-press the 0 button for +:
 
Jeton Shabani
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which OCA exam?
Good grief! Without proper training, you will find a cert exam very difficult. I am not at all happy to hear about your situation



i know... i am starting now.. 1° book: Head First Java  then OCA Study guide by Boyarsky and Selikoff and a lot of exercises.
"Without proper training" can you give me an example of proper training ?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now you come to mention it, I have a + on the 0 key too. It allowed me to enter +4412345678910 (or similar) and phone myself. Who's that sad character talking to himself

Tim H, I happen to live in one of the eight areas of Britain where I have to dial all eleven digits of every number, even local numbers. If I go six miles north, to Wolviston, where they have Sedgefield numbers, I only have to have to dial six digits from a landline. If I go 25 miles to Durham City, local numbers have seven digits. Where I used to live, local numbers have seven digits too, and in London, Southampton, Northern Ireland, etc., local numbers have eight digits.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeton Shabani wrote:. . . Head First Java  then OCA Study guide by Boyarsky and Selikoff . . .

Both good choices You know, one of the authors of HFJ (=Kathy Sierra) set up this website originally. We occasionally set Bert Bates and Scott Selikoff here, and Jeanne Boyarsky is active on this site, too.

Please confirm that you have the right editions of HFJ and Boyarsky and Selikoff for the exam they want you to sit.

. . . proper training ?

I had a good six months as part of an MSc course, then I discovered how much more I could learn from people on this site

Other people may have different opinions, so please wait and see what they say.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. My cell phone does also but my land-line does not.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:In most places in the world, a prefix of 00 indicates that the remaining digits follow are an international numbering plan (same as the + with E.164), and prefix of 0 indicates that the remaining digits follows a national numbering plan (usually an area/city code plus the local number).  Without either prefix, the digits follows a local or network-specific numbering plan.

In North America however, we use 011 as the prefix for international and 1 for national.


It's exactly for this reason I save all numbers in my phone using '+' as a prefix. This ensures that I will always call the intended recipient, no matter where I am.

I think this behavior distinguishes '+' from all other non-digits used in telephone numbers.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Stephan has made a point here. No telephone dial or keypad that I'm familiar with has a "+", but when written, it's a formal notation. Now that we have digital phonebooks, however, it can also serve as an indicator for validation and better processing.

Historical information on US phone numbers. Originally, calls were operator-assisted and each telephone exchange had a name. You can hear this on old-time radio plays like The Shadow, and in Ayn Rand's short story Anthem, where people had phone-number "names" to show how Socialist everything was (one of these days, I'm going to write a sequel to that work detailing what happens to people who think that they can survive out in the wilderness with zero external human aid).

This persisted until the early 1960s, even though the nation had mostly moved to automatic dialing. The exchange names all had 2-letter equivalents, so their direct-dialed equivalents could be found on the letters on the dial (which is why they were there). So, for example, we had EVergreen-9-1234 as a local phone number.

Not everyone liked this idea. Alan Sherman, most noted for "Hello Muddah, Hello Faddah" and other precursors to the Weird Al tradition recorded a lesser-known number entitled the "Let's All Call Up AT&T and Protest to the President March". "And he's all smirking and smiling because he's got us all digit dialing".

There's a whole 'nother level of strangeness in the history of the Tampa Bay area code 813, but I'll leave that for those who want to do their own research.

Speaking of area codes, as originally realized, area codes had a middle digit of "0" or "1". Not sure that still applies though. The 800-series codes (1-800 and 1-866 for example) are WATS (Wide Area Telephone Service), which generally meant that they carried no long distance charges. Long distance charges are pretty much extinct now, but back in the 1980's when dial-up BBS's were the rage, it was cheaper for me to dial boards in Kansas City and California than to call local long distance in my home state.
 
reply
    Bookmark Topic Watch Topic
  • New Topic