• Post Reply Bookmark Topic Watch Topic
  • New Topic

Numerology Program  RSS feed

 
Mason Reeves
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.


 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
okay so i have currently




Is this the correct approach?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
We covered a basic list of the math methods, not much else beyond that.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!