• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating characters  RSS feed

 
Karina Nersesov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I have a string that is an id number- this string has to be 8 characters long. If it does not have 8 characters then it is set to a default value of 00000000.
I decided to work with String not int because the substring would work on a string.

I have tried setting the id to an int and a string. Unfortunately when either are set the substring throws out a dereference error.
I do apologise if this is terribly confusing, I am just starting out with Java.

my code is as follows:


When I compile there is an error on the line int cannot be referenced. I can not figure out what to alter.

Thanks in advance.

 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if ( studentId.length() < length.length(8) ){.....

.....

studentId is int variable, meaning its primitive type. Its not a reference type, so you can't call methods on it...infact it doesn't have any methods to call.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Karina,
Welcome to Java Ranch

Please use code tags to post your code
I will do it for you now


First tell us what are you trying to accomplish, perhaps write a psuedocode or something like that
Also, the reason you are getting an error on line 8 means that int cannot be referenced, why it cannot be referenced because int is a primitive datatype for one and as per your code the variable studentId is merely a integer variable ,, its not an array or an object that is why it cannot be referenced,
Line 8 is indeed messy, do explain to us what you want to accomplish and we can discuss on how to get it resolved
Hope this helps
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
length variable should be initialized.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karina Nersesov wrote:my code is as follows:

Karina,

1. Welcome to JavaRanch.

2. Please UseCodeTags (←click) - and please read the page thoroughly. I've added them for you this time. See how much better it looks?

3. Your basic problem is that you're mixing up Strings with numbers. An int can't have a value of '00000000' (actually, it can, but it doesn't do what you think). For numeric types, 0 is 0 (or 0.0 for a floating-point type); there are no "leading zeroes".

4. Before you can parse your String into an int, you must first get it from the user. There are several ways to do this, but probably the easiest at this stage is to look at the Scanner class (java.util.Scanner ←click). In fact, the class allows you to get numbers directly. You may find this tutorial useful.

HIH

Winston
 
Karina Nersesov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I have a string that is an id number- this string has to be 8 characters long. If it does not have 8 characters then it is set to a default value of 00000000.
I decided to work with String not int because the substring would work on a string.

I have tried setting the id to an int and a string. Unfortunately when either are set the substring throws out a dereference error.
I do apologise if this is terribly confusing, I am just starting out with Java.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karina Nersesov wrote:Ok I have a string that is an id number- this string has to be 8 characters long. If it does not have 8 characters then it is set to a default value of 00000000.

Actually, I suspect that's not quite correct.

You are reading in an ID String, which must be:
(a) Numeric.
and
(b) 0-8 characters long. (and don't forget that 0; an empty String ("") has a length() of 0)

Now, you say: "if it does not have 8 characters then it is set to a default value of 00000000".
So, if I enter "456", you want that to default to "00000000"?

Or do you mean that if it contains less than 8 characters, you want to add leading '0's to make exactly 8 characters.
ie, if I enter "456", you want that to equal "00000456"?

First, you need to get that part sorted out; then we can deal with the rest of the problem.

But I'll tell you this: that ID looks very much like a number to me - actually, an integer.

All that stuff about "8 characters" and "00000000" is how you want to display it, which is something completely different.
The only thing they suggest to me is that your ID can't be < 0 or > 99999999.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!