• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write private static boolean method to throw invalidname exception.  RSS feed

 
karthik munukuntla
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should take uppercase,chracter, _ otherwise it should return false(doesnot match the namong style guides)



private static boolean checkNme(){
//Uppercase
//character
// _
otherwise
it should Show an error}
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Always use them: doesn't it look better . Don't ask the same question twice; I have deleted the othr copy.

There is something wrong with the design of your method. It does too many things at the same time. You are passing too much information. Why are you iterating a Map and seeing whether to throw an Exception and checking the name all in the same place? Those should be done in three different methods.
Why are you allowing invalid A2LObjects (now that is a poorly‑designed class name!) in the first place? Why are you not initialising the name field in the constructor and validating it there?

I cannot tell you how to validate the name because I do not know what structure of name you would require.
 
karthik munukuntla
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello campbell, Thanks for the reply. actually am working on a2l Parser.actually am new to programming. for parsing a file i created a main method. then created a html table to Display file values. now for the Parser created a class namechecker to check if it is upper case, character accept it or else return a error message.

as i mentione i am new to programming. help will be appreciated.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are going about it the wrong way. You need a concept of what you are going to do, a design.

I would still suggest that when parsing you validate the syntax/lexicality of the token when you create the A2LObject instance.
I would suggest that for parsing you will not want to change the state of the token as you go, so you may wish to make that class final and immutable. That tutorial example tells you about concurrency, but immutability is useful in other places too. If the fields of your A2L class are Strings and other immutable types, then it is easier to make it immutable because you don't need defensive copies.

Then when you have an object representing “karthik munukuntla”, it will represent “karthik munukuntla” for ever.

One way to validate a String is to see whether it matches a certain regular expression. The Java™ Tutorials contain a nice section about regular expressions, but I would allow not less than 24 hours to cover that section. You may find that your lexer is already using regular expressions to identify tokens, however.
 
karthik munukuntla
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you. this is my Output table. can you guide me how to write the code, if i type auppercase, character or _ in the Name filter it should accept or else it should return an error.
Capture.PNG
[Thumbnail for Capture.PNG]
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of format is that table? Is that code to parse? How reproducible are the contents of those boxes? What does the input look like?

We don't give people complete solutions here, but we try our hardest to help people.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does the input come from? Are you entering input from the keyboard?
 
karthik munukuntla
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the help. i have resolved the Problem by using regular Expression.



 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you have resolved the problem. You have found how to use a regular expression to verify the contents of the Strings (‍), but you can still put a non‑conforming String into the constructor and create an instance in an inconsistent state.
 
karthik munukuntla
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello campbell, how can i put a non-conforming string into aconstructor?

help will be appreciated
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new A2LObject("%$£&(*&^^$^%£$*&^(.,;@?><?}{")
 
karthik munukuntla
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!