Results 1 to 3 of 3
  1. #1
    matm is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Post Help with function "equals"

    Hello Java forum members!

    Instead of be on this forum and read others post of solved problems I registered and want to ask a question about Java that I have big problems with.

    Im doing a java crypto program in Java to school and in the beginning in my code I declerad an string array with alphabet letters.


    And now I wont the equals function to check the first letter in my word and "look" at the word in the "old" alphabet, when the program see it Im taking the same position on the cryptho alphabet.
    I hope you understand.

    Test:
    ------------------------------------------------------------------
    "Choose a cryptonumber between 0-29"!
    Then a method changing position with the number the user wrote in the beginning.
    Write your message in English..(the user write in the word)

    Now I put a for-loop to check the first letter in the word with the word in alphabet, when it match(equals) then I save the word in the crypt Alphabet.
    And continue with the second letter and so on. In the end I will have a crypt-word.
    ------------------------------------------------------
    The problem I have is with the for-loop that compare....

    I have put a lot of hours on this matter, checked charAt, toCharArray...String.....and so on. And really need help.

    Thanks in advanced..

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    public class Caeser {
    	
    		public static void main(String[] args) throws IOException {
    			
    			String [] realArray = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R",
    					"S","T","U","V","W","X","Y","Z","","",""}; 
    			String [] newArray = new String[realArray.length];
    			
    			Scanner scan = new Scanner(System.in);
    			FileReader fr = new	FileReader("vergene.txt");
    			Scanner infile = new Scanner(fr);
    			
    			
    			String numbers="", message="", CryptMessage="", crypt, newArr="", cryptNumber="";
    			int counting = 0;
    			
    			do
    			{
    				numbers = infile.nextLine();
    				System.out.print(numbers);
    				System.out.println();
    			}while(infile.hasNextLine());
    			
    		
    			int n = 30;
    			
    		
    			while(n > 29)
    			{
    				System.out.print("Choose a cryptonumber between 0-29");
    				n = scan.nextInt();
    			}
    			
    			int z = 0;
    			
    			for(int i=realArray.length-n; i < realArray.length;i++)
    			{
    				
    				newArray[z] = realArray[i]; 
    				//System.out.print(newArray[i]);
    				z++;
    								
    			}
    		
    			
    			z=n;
    			
    			for(int i=0; i <  realArray.length-n; i++)
    			{
    				newArray[z] = realArray[i];
    				//System.out.print(newArray[i]);
    				
    				z++;
    
    			}
    			
    		
    			for(int i=0; i < newArray.length; i++)
    			{
    				System.out.print(newArray[i]);
    			}
    		
    			System.out.println();
    				
    			
    			do
    			{	
    				if(counting != 0)
    				{
    					System.out.println(message + " contains wrong letter or letters!");
    				}
    				
    			System.out.println("Write your message in English");
    				
    			message = scan.next();
    			message = message.toUpperCase();
    			
    			counting++;
    			
    			}while(message.contains("") || message.contains("") || message.contains(""));
    			
    			String [] CryptoAlph = new String[message.length()];
    			String [] cryptMessage = new String[message.length()];
    			
    //THIS IS WHERE I HAVE THE PROBLEM. (Sorry for the upper case letters)
    			for(int i=0; i < message.length();i++)
    			{
    				for(int j=0; j < realArray.length; j++)
    					{
    						if(message(i).equals(realArray[j]))
    						{
    							cryptMessage[i] = newArray[j];
    							System.out.print(cryptMessage[i]);
    						}
    							
    					}
    			}
    	}
    //this loop ends here
    Last edited by matm; 12-25-2010 at 04:11 PM.

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Correct me if I'm wrong but I believe you are trying to loop through each character in each word and compare it to each element in realArray. In this case, you need a way to access each character in message and compare it to each String element in realArray. The charAt() method can be invoked on message but it will return a character, which must be converted to a String to be compared to realArray:

    for(int i=0; i < message.length();i++)
    {
    for(int j=0; j < realArray.length; j++)
    {
    if(Character.toString(message.charAt(i)).equals(re alArray[j]))
    {
    cryptMessage[i] = newArray[j];
    System.out.print(cryptMessage[i]);
    }

    }
    }
    --user0--

  3. #3
    matm is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    :)
    Thanks for your help, and it was like you say about the looping thing, but after a long time seeking on the internet, trying to understand what the fault is, I came to the answer with this code (its inside a for-loop):
    Java Code:
    if(String.valueOf(message.charAt(i)).equals(alphabet[j]))
    		{
    		     cryptMessage[i] = cryptArray[j]; //I renamed the variable
    	         }
    Really hard when I try to find the answer but when I see the answer in front of me it make sense. I think I need more Java-experience..

    I will never forget now about String.valueOf(......). It may be good help for me later on my future of Java coding.
    Last edited by matm; 12-28-2010 at 01:55 PM.

Similar Threads

  1. How correctly invoke function "round"?
    By artemff in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 11:31 AM
  2. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 12:46 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. "this" function in Java
    By mo_mughrabi in forum New To Java
    Replies: 5
    Last Post: 06-02-2008, 12:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •