Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: String in java

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String in java

    Greetings


    Consider in a Document if a String " Hello" is Encoded and stored as "XYZAB"

    I want to search the text on document for a word "Hello" and Replace the word with "HelloWorld"

    The Program will encrypt the word "Hello" and Search the file then return the encrypted code as "XYZAB" Found

    Now i have to replace the word "Hello" with "HelloWorld" in encrypted form so that the Letter "XYZABEFGHI" is replace in the place of Hello

    where "World" is encoded as "EFGHI"

    Now the Problem is If there is more number of occurrence of the word "Helloworld" exist in the file... How can i Replace only one particular occurrence What

    can be done to select the particular occurrence.



    Please Do Kindly Help me in sorting out the problem


    i have attached my java program for Encryption along with this mail for your ease of use.


    I Got struct here and cannot proceed further so please please kindly provide me a solution to proceed further...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: String in java

    Are you assuming how the encryption algorithm works? Will the String "hello" always be encrypted to "xyzab"?
    In a file, The second occurrence could be "asdfre" and the third "ertyu".
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: String in java

    What have you tried? What do you have so far?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String in java

    OUTPUT

    Enter the key :
    key
    PlainText : RAGU XYZ RAGU
    CipherText : BEEE VID BEEE



    package javaapplication7;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.util.Scanner;

    /**
    *
    * @author Ragunath Gunasekaran
    */
    public class JavaApplication7 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws Exception

    {

    Scanner in = new Scanner(System.in);
    Scanner file = new Scanner(new File("C:/Users/Ragunath Gunasekaran/Desktop/result.txt"));

    System.out.println("Enter the key : ");


    // TIMER START

    long startTime = System.currentTimeMillis();

    long total = 0;
    for (int i = 0; i < 10000000; i++)
    {
    total += i;
    }



    String key = in.nextLine().toUpperCase();

    String input="";
    while(file.hasNext())
    input+=file.nextLine().toUpperCase();

    System.out.println("PlainText : "+input);

    String cipher="";
    for(int i=0;i<input.length();i++)
    {
    char c = input.charAt(i);
    if(c>='A'&&c<='Z')
    {
    c=(char)(c+(key.charAt(i%key.length())-'A'));
    // if(c<'A')c+=26;
    if(c>'Z')c-=26;
    }
    cipher+=c;
    }

    System.out.println("CipherText : "+cipher);

    FileWriter fstream1 = new FileWriter("C:/Users/Ragunath Gunasekaran/Desktop/final.txt");
    BufferedWriter out = new BufferedWriter(fstream1);
    // cipher = br.readLine();
    out.write(cipher);
    out.close();

    String plaintext1 = "";
    for(int i=0;i<cipher.length();i++)

    {
    char c = cipher.charAt(i);
    if(c>='A'&&c<='Z')
    {
    c=(char)(c-(key.charAt(i%key.length())-'A'));
    if(c<'A')c+=26;
    // if(c>'Z')c-=26;
    }
    plaintext1 +=c;
    }

    // System.out.println("PlainText : "+plaintext1);

    // TIMER Run time & Print
    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    System.out.println("\n Runtime of the Ecryption Program "+elapsedTime);
    //TIME FINISH -Result Finish

    }


    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: String in java

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. String in java
    By mockingbird in forum Loops & Control Statements
    Replies: 1
    Last Post: May 14th, 2013, 07:08 PM
  2. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  3. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM
  4. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM