string 3 does not contain String 1 + String 2

  • Thread starter Thread starter AmiNeo
  • Start date Start date
  • Replies Replies 18
  • Views Views 3856

AmiNeo

CodeMonkey
AmiBayer
Joined
Jul 28, 2010
Posts
7,452
Country
UK
Region
Kendal, Cumbria
Hey guys I'm in the middle of a java assigment and I'm in a bit of a dialema...

Does anyone know how to get java to check if a string contains a combination of 2 other strings?

So far I have

boolean notTrue = false;

if ((!notTrue)&&((logins).contains(emailadd + ", " + password))) {
MainCourse loggedIn = new MainCourse();
loggedIn.setVisible(true);
}

But the above isn't working for some reason. Logins is a text file which is being read / streamed in as a string... that part works fine. The text file stores the email address and password as plain text as emailadd, password and I've copied and pasted the code exactly from the registration class. It still isnt working for some reason and I cant see why... It's likely something simple but I simply cannot see what.
 
Last edited:
My Java is rusty, I prefer C++ and C# but it should be something like this:

String s1 ="foobar";
String c1 = "foo";
String c2 = "bar";

return (s1.indexOf(c1, 0) != -1 && s1.indexOf(c2, 0) != -1);

--------------------------------------------------------
You cheated and added code while I was typing up my post. ;)

This code is probably more appropriate. Change this:
boolean notTrue = false;

if ((!notTrue)&&((logins).contains(emailadd + ", " + password))) {
to this:
if (logins.indexOf(emailadd + ", " + password) != -1) {
 
Last edited:
would doesnt look like it fits Java... The > than is normally only for comparing numerical values. Would .indexOf work with strings though? :unsure:
 
indexOf is a member of the String class and returns the starting position where the substring is found or -1 if it is not found.
 
Great! I'll give it a shot. :thumbsup:

Ps. I thought indexes were for arrays, but I guess theyre different functions.

---------- Post added at 06:23 ---------- Previous post was at 06:20 ----------

Doesn't like it :(

---------- Post added at 06:25 ---------- Previous post was at 06:23 ----------

For the record I'd much rather be learning C or C++ but were stuck with Java for now. :lol:

---------- Post added at 06:30 ---------- Previous post was at 06:25 ----------

It recognised it as a valid argument but still has the issue. I wonder if something else I've overlooked is causing it.

Basically its passing it by and going to the next if argument instead which is the same condition but without the match. The string IS definately in the text file though so it shouldnt be doing it.
 
try this:
int iResult;
iResult = logins.indexOf(emailadd + ", " + password);
if (iResult != -1) {

What is the value of iResult?
 
Nope, IDE recognises it but it overlooks it again. :picard

it must be something else...


Wish I wasnt such a noob...
 
Which IDE are you using?

P.S. I am staying online until this is solved as I am now determined to solve this.
 
rofl, its netbeans IDE. The one were told to use. :roll:

---------- Post added at 07:23 ---------- Previous post was at 07:14 ----------

Do you have an email address? Maybe I could send you the entire program to take a look at? It needs to be in Wednesday and I still have so much to do, need at least this bit fixing or its never going to be in on time, lol.
 
OK. I've the the JRE and JDK dowloaded and installed...waiting on NetBeans to download...
 
Here's the sample app I wrote:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author Craig
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String sLogins;
sLogins = "user1@test.com, 1234" + '\n' + "user2@test.com, 5678";

int iResult;

String sEmailAddress, sPassword;
sEmailAddress = "user1@test.com";
sPassword = "1234";

iResult = sLogins.indexOf(sEmailAddress + ", " + sPassword);

System.out.print (sEmailAddress);

if (-1 != iResult) {
System.out.println (" passed authentication.");
}
else {
System.out.println(" failed authentication.");
}
sEmailAddress = "user2@test.com";

iResult = sLogins.indexOf(sEmailAddress + ", " + sPassword);

System.out.print (sEmailAddress);

if (-1 != iResult) {
System.out.println (" passed authentication.");
}
else {
System.out.println(" failed authentication.");
}
}
}
Here is the output:
debug:
user1@test.com passed authentication.
user2@test.com failed authentication.
BUILD SUCCESSFUL (total time: 18 seconds)
Not sure what else to say. It performed as I expected. Can you post the entire function as well as the text of the login file?

---------- Post added at 02:21 ---------- Previous post was at 02:18 ----------

sent you my e-mail in pm.

---------- Post added at 03:21 ---------- Previous post was at 02:21 ----------

First, make sure the login file is being read. The first time I ran it the loop was never executing because the count was 0. I created a "user logins.txt" file in the root of your project and changed the location from "\\user logins.txt" to "user logins.txt".

Second I changed your if statement slightly:
if ((!notTrue)&&(emailaddr + ", " + password).equals(records)) {
to this:
line = records;
if ((!notTrue) && line.indexOf(emailaddr + ", " + password, 0) > -1) {
This worked as expected.
 
Here's the sample app I wrote:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author Craig
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String sLogins;
sLogins = "user1@test.com, 1234" + '\n' + "user2@test.com, 5678";

int iResult;

String sEmailAddress, sPassword;
sEmailAddress = "user1@test.com";
sPassword = "1234";

iResult = sLogins.indexOf(sEmailAddress + ", " + sPassword);

System.out.print (sEmailAddress);

if (-1 != iResult) {
System.out.println (" passed authentication.");
}
else {
System.out.println(" failed authentication.");
}
sEmailAddress = "user2@test.com";

iResult = sLogins.indexOf(sEmailAddress + ", " + sPassword);

System.out.print (sEmailAddress);

if (-1 != iResult) {
System.out.println (" passed authentication.");
}
else {
System.out.println(" failed authentication.");
}
}
}
Here is the output:
debug:
user1@test.com passed authentication.
user2@test.com failed authentication.
BUILD SUCCESSFUL (total time: 18 seconds)
Not sure what else to say. It performed as I expected. Can you post the entire function as well as the text of the login file?

---------- Post added at 02:21 ---------- Previous post was at 02:18 ----------

sent you my e-mail in pm.

---------- Post added at 03:21 ---------- Previous post was at 02:21 ----------

First, make sure the login file is being read. The first time I ran it the loop was never executing because the count was 0. I created a "user logins.txt" file in the root of your project and changed the location from "\\user logins.txt" to "user logins.txt".

Second I changed your if statement slightly:
if ((!notTrue)&&(emailaddr + ", " + password).equals(records)) {
to this:
line = records;
if ((!notTrue) && line.indexOf(emailaddr + ", " + password, 0) > -1) {
This worked as expected.


It actually auto creates the files on the fly if they dont exist when you register an account, that was working fine. The second part however was the issue... :thumbsup:

Does it log you in now that you have that in?? I was using and adapting code from another app I made earlier in the year... I must have missed a line out xD

---------- Post added at 10:01 ---------- Previous post was at 09:56 ----------

if you set line to equal records, will it not only read one line from the file? This program is intended to have many users registered...

Edit: nevermind, I see whats going on.. lol Thanks!
 
I did not debug beyond the login, I just made sure it would authenticate a user. It did iterate through the records array looking for a match.

line will contain a single record from the records array.
 
Ah, hey Andy! thats what I had in originally, or one of the things I tried anyways. It appears it may be an IDE problem or an issue unrelated to the comparison argument.

I've got a little help from moijk and he's given me what I need to iron out the issue... now if only I fully understood :lol:

I'm getting there at least :D
 
Why would it be an IDE issue? Surely the matter is between you and the compiler? :lol:
 
Shh I'm blaming netbeans! :blased: :lol:

In all seriousness oddlyits working now, I added a System.out.println("this is what is comparing: " + username + pass); to the bottom of the login, and oddly it now works??? :unsure: A simple print the values line shouldn't have altered the program but apparently it did. Either that or it's because I'm now doing it on a different PC... In which case it may be an IDE setup issue :P Maybe the libraries are corrupted or somethings up with the file...

Anyways emergency over I now have 36 hours to do 2 pieces of coursework. one of which is a java program, the other of which is a database... :picard


... The best bit is theres also a networking exam at the end of the 36 hours... :whistle:
 
Just thought I'd post an update for anyone reading this. I did what I could with this assignment and managed a pass! :D

We've been told unofficially whether we passed or not, and our full grades are in the post. These will be confirmed at the end of July but according to my tutors everything was plenty good enough! Just the external examiner needs to check them over

Thanks to everyone who offered this noob programmer with the issues! :thumbsup:


Now to spend the summer swatting up for next year! :D
 
Back
Top Bottom