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

Unexpected token

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, what i'm trying to do is compare 2 strings (username and password which are set in the variables) and then take the input typed into a jtextfield and jpasswordfield and then compare them. I've got most of it working except when I try to make the code go

JTextField -------> Checks String 1 and JPasswordField -------> Checks String 2

I end up with a error which says token expected.



My understanding is that the "||" checks that both conditions are true and if it is then the frame gets disposed and a new frame pops up. Can anyone guide me on this one?
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the number of circulat bracket here
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"||" checks that both conditions are true  


The OR operator (||) returns TRUE if either of the boolean expressions it is used with are true and it returns false if both of the boolean expressions are false.
The AND operator (&&) returns TRUE if both of the boolean expressions are true and false if either or both are false.

Do you want both tests to be true (use AND) or does only one (use OR) need to be true?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Token expected" is a compiler error which says (very obscurely, I agree) that something is missing in your code. It looks like Ganesh's post is right, your brackets aren't matching up correctly.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that guys, it was a long day and my head was all over the place so I managed to miss it! here is the revised code



The only issue now is that when I enter a string into the username and password fields it doesn't do as I wish



Am I missing something?
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • I was skeptical about this but I'm so lazy, didn't check this before my previous post.
  • Because passwordField.getPassword() return char[] and you are comparing char[] to String, do one thing just print the value which you get by doing  passwordField.getPassword() , you will know why it is not working.
  • Convert char[] to String by using one of the constructors of String i.e. String(char[] value), store this in a String variable then use that variable to check equals.
  • I would actually store both user name and password String in String variables and validate for empty String, max length ex user name max 30 char and password max length 20 etc.
  •  
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey I just had a look at what you said and i'll be completely honest with you, it doesn't make too much sense to me. Could you break it down abit more without actually answering my question by posting code because I want to figure this out by myself. I'll post what I have but I feel like what is getting me is getPassword returns a char[] but what to do next. I'll post my code and if you guys can help me out that would be greatly appreciated.



     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    mitchell bat wrote:getPassword returns a char[] but what to do next.


  • Yes right your going correct, now you want to check whether that password matches to the value of passwordString variable right? so as you know you get char[] by using getPassword, you can't use equals method on char [] variable then you first have to convert that char[] into String.
  • How to convert that? If you know String class has a constructor which takes char[] as parameter using that you can have a String value of all those characters present in char[].
  • In your example hereNow you have to invoke equals method on passwordString to check it with specified String i.e. userString
     
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:"Token expected" is a compiler error which says (very obscurely, I agree) that something is missing in your code. It looks like Ganesh's post is right, your brackets aren't matching up correctly.

    Which compiler are you using? Eclipse?

    You shou‍ld use an editor which supports bracket matching; hover your mouse over the first ( and its paired ) should change colour. I think that works on IDEs too.
     
    Saloon Keeper
    Posts: 15491
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There's a reason that passwords are kept in char arrays. Sensitive data shouldn't be stored as strings. For now, what Ganesh suggested will work, but applications that are more than just practice projects should never convert passwords to strings.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You shou‍ld find out about the security problems with passwords, but databases maintain passwords as a hash of some sort. You shou‍ld use this sort of method to compare equality of the char[]s.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Would I use toString method? that's the only thing I can think of right now and when you say

    If you know String class has a constructor which takes char[] as parameter using that you can have a String value of all those characters present in char[].
    In your example here



    Have I given it the right constructor?
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Which compiler are you using? Eclipse?




    I'm using intellij
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Doesn't IntelliJ use ordinary javac behind the scenes? I would have thought you probably get bracket highlighting on IntilliJ.
    I have given you a suggestion about how to check equality of two char[]s. You can use new String(myCharArray) or the toCharArray method of the String class. As I said, I think much production code uses an SHA512 or something similar to store a hash of your password, but I am not certain. You can find that out in a security book or maybe Wikipedia.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I would have thought you probably get bracket highlighting on IntilliJ



    It does but because of the theme that i'm using it makes it difficult

    You can use new String(myCharArray) or the toCharArray method of the String class



    It's getting late where I am but when I get a chance tomorrow i'll check it out and see if I can put it together

    As I said, I think much production code uses an SHA512 or something similar to store a hash of your password



    I'll read about it when I can because i'm always curious about java and programming


    The support on this forum is amazing and just wanted to extended a big thanks to everyone who participates in keeping this forum going

     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    mitchell bat wrote:Have I given it the right constructor?

    Yes you used correct constructor but I would go with advices of Stephan van Hulst and Campbell Ritchie and use char[]. You can use for practice project as Stephan van Hulst mentioned. If you are interested in knowing why prefer char[] over String for password I'll post the link of thread here which I'm going to create for that issue.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I've had a go and can't work it out. I'll explain my logic and what i've done

    Here what i've done is created a string for the username to be used, i've created an char array and a string that holds the password



    and now here what I think iv'e done is get the input from the password field and used the get password method to change it into a char and then used the toString method to convert the char to a string and compared it back to the string that I have by using the euqals method




    But still not working, please tell me that i'm close guys!
     
    Paul Clapham
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Have you validated your assumption about what

    returns?
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    to know what is happening in your code, whenever necessary you better print the value of variables or whatever you get to keep track of it. If I don't know what returns then As Paul already mentioned, I need to know what it returns but how? print it's returned value.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Have you validated your assumption about what
    ?
    1
    passwordField.getPassword().toString()

    returns?



    I've tried to google java validation but not much comes up, can you post a tutorial so I can have a read through it and hopefully come up with a solution.

    But just out of curiosity why am I not getting this to work? Is it not working because of the validation or what?
     
    Paul Clapham
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A tutorial???

    You seem to have assumed that you know what the result of



    would be. I'm suggesting that your assumption is wrong. To test the assumption all you need is to display the result and see if it matches your expectation. To do that:



    You don't need a tutorial to find out about System.out.println, the simplest form of debugging.
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    he meant to say that did you check what you get by doing thisDo one thing print the value like this and see what it prints
    Does that print contents of char[] array as String? or something else ?
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So I ran this code and the output of the validation was

    [C@5c50750f



    and then I did it again and I got this

    [C@62530b2c



    Which means i'm doing something wrong, which by the looks of things it's not being converted into a string. Am I right in saying that?
     
    Paul Clapham
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You aren't quite right. The toString() method always produces a String, it can't do anything else. However you have assumed that if you have an array of chars, then its toString() method will produce a String which concatenates all the chars in the array. But as you can see now, that isn't the case. So yes, it produces a String. It just doesn't produce the String you need.

    If you want to convert an array of chars into a String according to the blindingly obvious rule, then calling the array's toString() method isn't the way to do it. There's a String constructor which does what you want, so check the API documentation for String to find it.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm stick stuck and confused but the only thing I can think of is by using this constructor

    http://www.tutorialspoint.com/java/lang/java_lang_string.htm



    Number 8, am I on the right track?
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes this constructor String(char[] value), You better use official source public String(char[] value)
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Still no luck but I feel like i'm close



     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is it meant to be going in the constructor of a class or of the instance ?
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are suppose to pass char[] varaible only i.e. c in your code and invoke equals method on passwordString variable to check whether it is equal to the value in userString like this I think you should read Java Tutorial and do some program to understand it properly else just providing solutions not going to work.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Surely you should not keep passwords in String format? Use the equals method I showed earlier and maintain all passwords in char[] format.
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And clear char[] after use by setting each character to zero for stronger security.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes your correct, well done.  Now  you don't have to convert char[] to String. No need of this code
    I think below code was just to verify output so don't forget to remove that also
     
    You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic