Results 1 to 3 of 3
Thread: Help with function "equals"
- 12-25-2010, 04:06 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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.
I´m 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 I´m 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 hereLast edited by matm; 12-25-2010 at 04:11 PM.
- 12-27-2010, 03:34 AM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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--
- 12-28-2010, 01:50 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
:)
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):
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..Java Code:if(String.valueOf(message.charAt(i)).equals(alphabet[j])) { cryptMessage[i] = cryptArray[j]; //I renamed the variable }
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
-
How correctly invoke function "round"?
By artemff in forum New To JavaReplies: 2Last Post: 01-01-2010, 11:31 AM -
How "Pattern.matches(regex, input)" function works?
By kishan in forum Advanced JavaReplies: 2Last Post: 04-26-2009, 12:46 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"this" function in Java
By mo_mughrabi in forum New To JavaReplies: 5Last Post: 06-02-2008, 12:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks