return string upon entering one of the previously saved Strings.

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1026 Views - Last Post: 14 February 2010 - 10:02 AM Rate Topic: -----

#1 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

return string upon entering one of the previously saved Strings.

Posted 06 February 2010 - 06:35 AM

Hi everyone!

Last time this community helped me a lot - thank you one more time.

I have another problem. This is what I would like to achieve:

1.) User enters some codes
2.) the program stores them in an array of strings
3.) after entering some codes, the user is asked to enter a previously 'saved' code
4.) if the string entered is ANY of the previously entered codes, the program
prints out 'chicas bonitas'

My problem: I can only get it to work for the last code entered, how do I get it right?

Thanks in advance for any help!

My code:


 {

      Scanner input= new Scanner(System.in);


	 	String[] code = new String[100];
		String codex, answer;
		int i = 0;

		System.out.print("\nHello! Would you like to enter a code?  (Y/N): ");
			         answer = input.nextLine();

					while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
					 System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
					 System.out.print("Hello! Would you like you enter a code?  (Y/N): ");
					 answer = input.nextLine();
					            }

					if(answer.equalsIgnoreCase("N")){System.out.println("\nGoodbye!\n");}

					while(answer.equalsIgnoreCase("Y")){

			         System.out.print("\nplease enter a code: ");

	             code[i] = input.nextLine();

	             System.out.print("\nThanks for entering the code:\n" + code[i]);
				i++;

				System.out.println("\nWould you like to enter a code? (Y/N): ");
					             answer = input.nextLine();
					             while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
								 			 System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
								 			 System.out.print("\nWould you like to enter a code?  (Y/N): ");
								 			 answer = input.nextLine();
			            }
					}
		     System.out.print("\nplease enter a previously saved code: ");

		     	         codex = input.nextLine();

		     if(codex.equals(code[i-1]))
	             {System.out.println("CHICAS BONITAS");}
	             }
			 }





Is This A Good Question/Topic? 0
  • +

Replies To: return string upon entering one of the previously saved Strings.

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: return string upon entering one of the previously saved Strings.

Posted 06 February 2010 - 07:11 AM

that is because this is the only check you perform.
you only check if the entry is equals to the last code entered with this check:
if(codex.equals(code[i-1])) //<<<<<<code[i - 1] is the last String object in code array.



in order to go over all the String objects the user entered, iterate the array using a for loop. the variable i now holds the number of String objects entered, so the loop would look something like:
for(int j = 0 ; j < i; j++)
{
    if(code[j].equals(codex))
    {
         System.out.println("Code found");
         break;
    }
}



Hope i could help :)
Was This Post Helpful? 1
  • +
  • -

#3 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: return string upon entering one of the previously saved Strings.

Posted 06 February 2010 - 07:17 AM

That's it! I just didn't know how to construct that for loop, I created a program earlier that was using a while loop here, but it was a non-terminate.

THANK YOU!
Was This Post Helpful? 0
  • +
  • -

#4 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: return string upon entering one of the previously saved Strings.

Posted 07 February 2010 - 02:11 AM

Hard to be a noob :)

I have a follow-up question.

What I would like to achieve now:

1.) In addition to the previous program, when the user
enters a code that he had not entered before it should print out
a msg 'no such code in the DB'.
2.) And as a next step enable the user to reenter a code and
repeat the process, so that if he made a mistake the first time around,
he can try entering it again.

My problem here:
It works fine, with the exception, that the program recognizes the code
correctly the first time around, if the user made a mistake however,
reentering the correct code leads to nothing, as the program 'loses'
knowledge about the code.

Could you tell me what I am doing wrong?
Thanks in advance

CODE:


import java.util.Scanner;

class chicas{

    public static void main(String args[])

    {

      Scanner input= new Scanner(System.in);


	 	String[] code = new String[100];
		String codex, answer;
		int i = 0;

		System.out.print("\nHello! Would you like to enter a code?  (Y/N): ");
			         answer = input.nextLine();

					while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
					 System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
					 System.out.print("Hello! Would you like you enter a code?  (Y/N): ");
					 answer = input.nextLine();
					            }

					if(answer.equalsIgnoreCase("N")){System.out.println("\nGoodbye!\n");}

					while(answer.equalsIgnoreCase("Y")){

			         System.out.print("\nplease enter a code: ");

	             code[i] = input.nextLine();

	             System.out.print("\nThanks for entering the code:\n" + code[i]);
				i++;

				System.out.println("\nWould you like to enter a code? (Y/N): ");
					             answer = input.nextLine();
					             while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
								 			 System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
								 			 System.out.print("\nWould you like to enter a code?  (Y/N): ");
								 			 answer = input.nextLine();
			            }
					}
		     System.out.print("\nplease enter a previously saved code: ");

		     	         codex = input.nextLine();

			for(int j = 0 ; j < i; j++)

		{
				 if(code[j].equals(codex)){System.out.println("CODE IS: " + code[j]);

					System.out.println("\nplease enter another previously saved code: ");
					codex = input.nextLine();

			     }
			     if(!code[j].equals(codex))
			     {
			         System.out.println("\nNO SUCH CODE IN THE DB\n PLEASE ENTER ANOTHER ITEM CODE: ");
			         codex = input.nextLine();
			     }
}
}
}



Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: return string upon entering one of the previously saved Strings.

Posted 07 February 2010 - 05:19 AM

i noticed that your check if a code is not in the db is not yet complete.
i would suggest add a method isValid(string[] codeDB, string code, int length) this method gets a code from the user, and the string database, and the database current length. and checks if the code is in the db.
here is the idea:
public static boolean isValid(String[] codeDB, String code, int length){
   for(int i = 0 ; i < length, i++)
   {
         if(codeDB[i].equals(code)){
            return true;
         }
    }
    return false;
} 



now, in the program, just call that method everytime you want to valid an input.
supply it those parameters:
code (as the codeDB)
codex (as the code)
i (as the length)

also, just an advice, call your variables in meaningfull names. this way, if you dont look at your code for a week, when you go back to it, it would be easier for you to understand.
and easier for others reading it. :)

This post has been edited by japanir: 07 February 2010 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: return string upon entering one of the previously saved Strings.

Posted 07 February 2010 - 09:39 AM

@Japanir: I think you might be working in C or C++, since Java doesn't have a bool datatype, and String is captialized. :)

Edit: Added a space between "a" and "bool"

This post has been edited by macosxnerd101: 07 February 2010 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#7 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: return string upon entering one of the previously saved Strings.

Posted 07 February 2010 - 10:28 AM

@Japanir: Many thanks for your help!

Since I am new to programming (2-3 weeks of experience) - I tried to read Sun's tutorial relating to the code you provided.
I get how you defined it and understand it's purpose..

.. I do not have any clue how to use it in my program.

Could you explain it to me on an example?

I would be very very grateful.

PS. Good tip with the var names - will do so from now on.
Was This Post Helpful? 0
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: return string upon entering one of the previously saved Strings.

Posted 07 February 2010 - 11:51 AM

:stupid:

Thanks macosxnerd101 for pointing those mistakes :)
i will also edit that post.
Was This Post Helpful? 0
  • +
  • -

#9 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: return string upon entering one of the previously saved Strings.

Posted 07 February 2010 - 12:59 PM

Did you see my last post?

Don't want to be harassing you, but I don't think you'd just ignore it :)

View Postjapanir, on 07 February 2010 - 10:51 AM, said:

:stupid:

Thanks macosxnerd101 for pointing those mistakes :)
i will also edit that post.

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: return string upon entering one of the previously saved Strings.

Posted 07 February 2010 - 01:22 PM

@Japanir: Not a problem. You had the right concept, but the wrong language. :)

@IncredibleDissapointment: So basically, somewhere in your class, you have the method Japanir wrote. It returns a boolean value- true or false- based on whether or not the String param is contained within the first length elems of the String[] param. So if you invoke the method as such:
//the array to search through
String[] array = "Hello World! My name is Macosxnerd101".split(" ");

//the term to search for
String searchFor = "Hello";

//if isValid returns true for the params
//invoke isValid to search through array for searchFor in the entire array
if(isValid(array, searchFor, array.length)) 
  System.out.println("Found!"); //then print found

//otherwise, print not found
else System.out.println("Not Found");


Was This Post Helpful? 1
  • +
  • -

#11 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: return string upon entering one of the previously saved Strings.

Posted 10 February 2010 - 02:10 PM

Hi again!

I tried doing today what you fine gentlemen said.

I didn't have Internet access for 3 days.

I spent 6 hours on that, applying different solutions,
in the end I got back to your idea, unfortunately, I am
apparently hopeless at doing it right..

I put so much work into it, but just got very, very frustrated.

What am I doing wrong?

Please, help me further, I REALLY would like to know the answer to this!

(I havn't changed the var names as of yet - not to get confused)

The code:

import java.util.Scanner;

class chicas{

    public static void main(String args[])

    {

      Scanner input= new Scanner(System.in);


String[] code = new String[100];
		String codex, answer;
		int i = 0;
		int length = 100;
		boolean valid = true;

		System.out.print("\nHello! Would you like to enter a code?  (Y/N): ");
			         answer = input.nextLine();

					while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
					 System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
					 System.out.print("Hello! Would you like you enter a code?  (Y/N): ");
					 answer = input.nextLine();
					            }

					if(answer.equalsIgnoreCase("N")){System.out.println("\nGoodbye!\n");}

					while(answer.equalsIgnoreCase("Y")){

			         System.out.print("\nplease enter a code: ");

	             code[i] = input.nextLine();

	             System.out.print("\nThanks for entering the code:\n" + code[i]);
	             i++;


				System.out.println("\nWould you like to enter a code? (Y/N): ");
					             answer = input.nextLine();
					             while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
								 			 System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
								 			 System.out.print("\nWould you like to enter a code?  (Y/N): ");
								 			 answer = input.nextLine();
			            }

					}

System.out.println("\nplease enter a previously saved code: ");

	             codex = input.nextLine();

	             if(isValid(String[] code, String codex, int length)) { System.print.out("found!");}




}
public static boolean isValid(String[] code, String codex, int length){
   for(int i = 0; i < length; i++)
   {
         if(code[i].equals(codex)){
            return true;
         }
    }
    return false;
}

}


Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: return string upon entering one of the previously saved Strings.

Posted 10 February 2010 - 02:50 PM

Can you describe specifically the error(s) you are getting? Also, please work on your indentations for your code, as right now, it is very difficult to follow the flow of the code. Thanks for helping us help you!
Was This Post Helpful? 1
  • +
  • -

#13 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: return string upon entering one of the previously saved Strings.

Posted 11 February 2010 - 02:52 AM

Sorry for the indentations! I include a better version :)

Thanks again for your quick reply!


EDIT: THANK YOU FOR YOUR HELP! I GOT IT TO WORK, combining both ideas more or less,
using this:
for(i=0;i<code.length;i++) {
	if(codex.equals(code[i])) {
    found = true;
	break;
}
}

if(found) {System.out.println("found! the code is: " + code[i]);
}
else { System.out.println(codex + " is not in the code database!");
}
}
}




I get following error msgs:

'.class' expected
if(isValid(String[] code, String codex, 100)) {
^
';' expected
if(isValid(String[] code, String codex, 100)) {
^
<identifier> expected
if(isValid(String[] code, String codex, 100)) {
^
illegal start of type
public static boolean isValid(String[] code, String codex, 100){
^
Now if I leave "int length" I get the compiler error '; expected' and 'identifier expected'.
If I put var types in parentheses - I get only 1 error - illegal start of expression by (is Valid((String[]...
^
I tried to do it without the method you gave me many times as well,
and the main problem is, that even if i get it to work, if a code is found,
it prints out code found AND code not found at the same time - following
the (for i = 0, i < 100, i++)loop, it just analyses which code[i].equal(codex),
and which not - so that I get prints for code found, whenever it is found, and not found
when not, I just would like to receive either code found, and which code it was,
or code not found if the code entered isn't matching the previous code.

Hope that I didn't make it too confusing :)

import java.util.Scanner;

class chicas{

public static void main(String args[])

{

Scanner input= new Scanner(System.in);


String[] code = new String[100];
		String codex, answer;
		int i = 0;
		int length = 100;
		boolean valid = true;

System.out.print("\nHello! Would you like to enter a code?  (Y/N): ");
 answer = input.nextLine();

while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
System.out.print("Hello! Would you like you enter a code?  (Y/N): ");
 answer = input.nextLine();
	 }

if(answer.equalsIgnoreCase("N")){System.out.println("\nGoodbye!\n");}

while(answer.equalsIgnoreCase("Y")){

System.out.print("\nplease enter a code: ");

code[i] = input.nextLine();

System.out.print("\nThanks for entering the code:\n" + code[i]);
i++;


System.out.println("\nWould you like to enter a code? (Y/N): ");
 answer = input.nextLine();
while(!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")){
System.out.println("\nIncompatible answer. Please enter either Y or N!\n");
System.out.print("\nWould you like to enter a code?  (Y/N): ");
 answer = input.nextLine();
	 }

   }

System.out.println("\nplease enter a previously saved code: ");
codex = input.nextLine();

if(isValid(String[] code, String codex, 100)) {
System.print.out("found!");}
}
public static boolean isValid(String[] code, String codex, 100){
for(int i = 0; i < length; i++)
{
if(code[i].equals(codex)){
return true;
 }
}
return false;
}

}




Here is what I tried to implement instead of the method, but couldn't get it to ONLY display ("not found!"),
when no such item was found..

for(i=0;i<code.length;i++) {
	if(codex.equals(code[i])) { System.out.println("found! the code is: " + code[i]);
	break;
}

if(!codex.equals(code[i]) && codex.equals(code[i])) {System.out.println("found! the code is: " + code[i]);
}
}
if (!codex.equals(code[i]) {System.out.println("not found! ");
}
}
}


This post has been edited by IncredibleDisappointment: 11 February 2010 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#14 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: return string upon entering one of the previously saved Strings.

Posted 11 February 2010 - 04:04 AM

here is the problem:
in the declaration of the method, you cannot pass a constant as parameter.
public static boolean isValid(String[] code, String codex, 100) //<<<<100 is an int constant


you will have to replace it with a parameter.
so change it to:
public static boolean isValid(String[] code, String codex, int length)



then the method will look like:
//now your compiler will know what is length
public static boolean isValid(String[] code, String codex, int length{ 
for(int i = 0; i < length; i++) 
{ 
if(code[i].equals(codex)){ 
return true; 
 } 
} 
return false; 
} 



now, another mistake,
if(isValid(String[] code, String codex, 100)) { 
System.print.out("found!");
} 


when calling a method, you will have to pass the variables you want to work on as parameters dont pass their datat type.
just pass code, codex, as these are the variables (you don't have to pass their data type too)

so call your method this way:
if(isValid(code, codex, 100)) 


also, the reason i recommended on passing length as parameter, is to pass the "logic" length of the code db.
since you declare on a length of 100 String Objects, and then just get user input to fill the array. so you will not necessarily have 100 objects. then it is useless to check 100 String objects.
for that, pass "i" which counts the number of inputs. so you will only have to check number of inputs objects.
so pass i as:
if(isValid(code, codex, i)) 


Was This Post Helpful? 1
  • +
  • -

#15 IncredibleDisappointment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-December 09

Re: return string upon entering one of the previously saved Strings.

Posted 11 February 2010 - 04:53 AM

Thank You! You helped me a lot, I got it to work the other way, but using some
of your tips.

Thank you for explaining method calling to me, I read some additional tutorials,
seem to get a hang of it.

I have another small question,
please, if you could help me one last time in this thread, read my code,
and tell me, why the part after if (found=false) is NOT displayed in the program,
the program, after entering a code, that is not in the 'database', shows
'would you like to place an order?', instead of "codex +" was not found in the database"";
it just skips what i wrote after if (found=false).

The code (I just copied the relevant part):

This post has been edited by IncredibleDisappointment: 11 February 2010 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2