• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Replacing and checking character in string

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

so my task is to write a code which would ask user to input the year as integer and first three letters of the month with first being an upper case letter. Than the program would determine the number of days for given year and month.
Currently I have a brain crash on how to check, if the user has provided the month with first character being upper Case. If not, than the program would automatically correct the character. Problem starts at line 17.



thanks for help
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just curious why does the month needs to have the first letter capitalize?

I'm thinking, if the user enters "jan", "Jan", "JaN" or anything like it, the code can change all to uppercase or lowercase before you feed that into the switch/case.

Better yet, have you consider using the constants in Calendar eg Calender.JANUARY for january? This way the code will be sure the input is a month. By the way you will need to convert the user's input say "jan" to Calendar.JANUARY.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lovro Bajc wrote:If not, than the program would automatically correct the character. Problem starts at line 17.


You're right. Here's a suggestion. Instead of using a char, which doesn't have any methods, how about this instead:
Character ch = monthName.charAt(0);
?

I'll leave the rest to you; see if you can come up with a solution (you might want to read the API for java.lang.Character while you're at it).
And if you run into problems, come back.

You might also want to think about breaking out some of that code into methods as well. That switch statement, for example, is very nice; but I suspect you'll find it much more useful in a method that does something for you (daysInMonth(String month) ?).

Winston
 
Marshal
Posts: 79968
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't mess around with chars. Simply turn the month to lower case and stick with jan/feb/mar. What you have written cannot change the String entered, anyway.

Or use fall‑through:-You can read about fall‑through in the Java Tutorials. If you do use fall‑through, you must add a comment about what you are doing. Otherwise people maintaining your code will assume it is a mistake and add break; statements.

By the way, the formatting in that post is dreadful. You are mixing tabs and spaces (don't), and presumably all the multiple statements squashed into single lines are there to confuse us into not noticing how very long your main method is. Well, it hasn't worked But it is confusing.

Those multiple print statements are not at all good. You have written the same thing twelve times. Use one print statement after the switch.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for quick response. Till now, I have not learned methods or Calendar class. I am a beginner and a self-learner I will take a look into what You have suggested and will most likely be back :=) thanks for the help so far.

Animus
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Lovro Bajc wrote:If not, than the program would automatically correct the character. Problem starts at line 17.


You're right. Here's a suggestion. Instead of using a char, which doesn't have any methods, how about this instead:
Character ch = monthName.charAt(0);
?

I'll leave the rest to you; see if you can come up with a solution (you might want to read the API for java.lang.Character while you're at it).
And if you run into problems, come back.

You might also want to think about breaking out some of that code into methods as well. That switch statement, for example, is very nice; but I suspect you'll find it much more useful in a method that does something for you (daysInMonth(String month) ?).

Winston



I guess just a simple in line 17 correction with your help f:D



 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lovro Bajc wrote:so my task is to write a code which would ask user to input the year as integer and first three letters of the month with first being an upper case letter. Than the program would determine the number of days for given year and month.


Just FYI, here's a little technique you may not be aware of:

Enums are wonderful things, and can do an awful lot of neat stuff for you, because they're just like normal objects:Now it might not look like much, but if you use it in your code:
  • Month.of("Apr").days(1973); will return 30.
  • Month.of("Feb").days(2000); will return 29.
  • Month.of("Feb").days(1900); will return 28.
  • Month.of("Jan").index(); will return 0.
  • Month.of("Dec").index(); will return 11.
  • which you may find quite useful.

    However, K.Tsang makes a good point - the "capital letter" is kind of redundant and just adds complexity. Personally, I like to use all-caps for my enum names (and you can easily convert a String to upper-case), but it's not a requirement.

    HIH

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 79968
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What a nice enum.
    I always wondered how you got the days out of February: now I know. Thank you
    By the way: will ordinal() give you 1 for January or 0 for January?

    If anybody is not familiar with enums, read about them in the Java Tutorials.
     
    Bartender
    Posts: 3323
    86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:By the way: will ordinal() give you 1 for January or 0 for January?


    The first enum declaration has an ordinal value of 0.
     
    Campbell Ritchie
    Marshal
    Posts: 79968
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's what I thought.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:What a nice enum.


    Cheers tosh. As you may have gathered, I really like enums.

    By the way: will ordinal() give you 1 for January or 0 for January?


    0. At least according to the docs.

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

    Winston Gutkowski wrote: . . . I really like enums. . . .

    I presume that is because enums are bl**d* good things? I like them too.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I presume that is because enums are bl**d* good things?


    Certainly are.

    Just one of the many things that people forget about them is that they can implement interfaces, and I've used precisely that idea to create an Index class that "combines" Comparators so that, for example, you can sort books by Author, then Title. It doesn't rely on it, but it provides all sorts of extra goodies if you keep all your "Book Comparators" in an enum.

    Winston
     
    Lovro Bajc
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What would than be idea behind checking if the string of entered digits contains only digits or just characters?
    I know that in order to get a sub-string out you need to define range where the string starts and ends. However to include
    method .isDigit does not work...

    and also how to compare the entered character "-" in a string?

     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lovro Bajc wrote:What would than be idea behind checking if the string of entered digits contains only digits or just characters?
    I know that in order to get a sub-string out you need to define range where the string starts and ends. However to include
    method .isDigit does not work...


    As far as valid numbers are concerned, Scanner already allows you to do that (this, from the wide world of Campbell):
    Unfortunately, if you want to also validate that it's within a range, you have to add another loop around it. Alternatively, you could have a look at the UserInput page, which goes into the whole business of user input in more detail (including entering ranges). but I warn you: it's not short.

    and also how to compare the entered character "-" in a string?


    Not quite sure what you mean. Do you mean a leading "minus" sign? If so, don't worry about it - hasNextInt()/nextInt() will deal with that for you.

    PS: Sorry about all the other discussion here. It's Friday.

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

    Winston Gutkowski wrote: . . . from the wide world of Campbell): . . .

    While the acknowledgement is appreciated, it was Rob Spoor who showed me that technique.
    I recommend you put something like this in the loop, too:-
     
    Campbell Ritchie
    Marshal
    Posts: 79968
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote: . . .
    Unfortunately, if you want to also validate that it's within a range, you have to add another loop around it. . . .

    Which is why I often tell people to write themselves utility classes.

    I shall have to write a keyboard input No 2 FAQ
     
    My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic