• 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

Numerology Program

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

This is my first post on the forum, so sorry if I don't follow the norms yet around here. I really need some help on this program I am trying to write for my beginner's java class I'm taking at school. It is a numerology project where we read a date from the user, validate the date, and crunch the numbers down to a single digit (1-9) by adding the single digits of the date together. The part I'm struggling with here is reading the input from the user in one line. I can't tell why I am getting a runtime exception when I run the program. Any help would be really appreciated, I am really struggling with this one.


 
Marshal
Posts: 8970
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mason Reeves wrote: I can't tell why I am getting a runtime exception when I run the program.


Hi,

When you run the program you shouldn't get any exception. You should get exception, when you try to input date, because probably you're trying to add date in a format for ex. 30/04/1975, but line above expects int type and character "/" is not of that type.
Lines 10,12 are not necessary, as you're not using them and not really clear the purpose of it.

What you could do, just try to read the date as a string, and use the function split, so finally you would get the date in the array (withoug "/" symbols). After that need to thing, what would you like to do next.
 
Mason Reeves
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I read the date as a string is it possible to tell the program which part of the string is the month, day, year, and slashes?
 
Liutauras Vilda
Marshal
Posts: 8970
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example if user would input 30/04/1975, after you split that string with "/", you'd get an array of 3 elements:
Array[0] - 30
Array[1] - 04
Array[2] - 1975

I'm not sure what you mean

Mason Reeves wrote:...is it possible to tell the program which part of the string is the month, day, year, and slashes?


I'll take the risk and will say - yes, you can specify which element of the array is considered as day or month or year of the birth.
 
Mason Reeves
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I'm probably not looking to be the brightest bulb right now. I struggle a lot with java. We haven't gone over arrays in our class, so I'm not permitted to use arrays in my program. I just need to be able to read the integer values as well as the slashes in one line so I can validate the date and crunch it. I just have no idea how to get the scanner to properly read both the slashes and integers in one line.
 
Liutauras Vilda
Marshal
Posts: 8970
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mason Reeves wrote:...I'm not permitted to use arrays in my program. ...how to get the scanner to properly read both the slashes and integers in one line.


The only way in your wannabe way to read "number" and "/" is to read it as a String.
You still can retrieve each character later on by calling charAt method on that string, but i'm not sure it is what you want.
 
Mason Reeves
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay so i have currently




Is this the correct approach?
 
Liutauras Vilda
Marshal
Posts: 8970
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure, why you're trying to store slashes, why do you need them?
Beside that, i'm afraid it can become really complicated, because of your limitation of using methods, which you haven't covered in your class yet.
Do you really need to use "/" in the date input?
 
Mason Reeves
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, it is required that the date be in the format mm/dd/yyyy. if the user doesn't input slashes, I am to output a statement that tells them to use slashes between their numbers. That is why I want to store the slashes. We have gone over using multiple methods in a program in class, I'm just not sure how that could be used in this scenario.
 
Liutauras Vilda
Marshal
Posts: 8970
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what methods did you cover and what data types?
To read the date in a format: "mm/dd/yyyy", it cannot be an int at first instance.
 
Mason Reeves
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We covered a basic list of the math methods, not much else beyond that.
 
Liutauras Vilda
Marshal
Posts: 8970
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so, we know, that date format specified exactly in "mm/dd/yyyy" format, cannot be stored as an "int" type at first instance because of "/" characters.
Which data type could hold this wannabe format value?
 
Marshal
Posts: 79979
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you make the Scanner use / as part of its delimiter? If you try to print out the delimiter
System.out.println(new Scanner(System.in).delimiter());
you get this:

\p{javaWhitespace}+

That means 1 or more whitespace characters according to whatever that definition of whitespace is.
So pass an instruction to take a slash instead.
myScanner.useDelimiter"\\p{javaWhitespace}+|/");
\ is a metacharacter, so it must be escaped to \\. / is not a methcharacter so it doesn't need escaping. The | means or, so you can use space or / as a delimiter.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
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