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 3 of 3

Thread: I need a little help

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry I need a little help

    I need a little help, I have some depreciated code, and I have no idea how to change it to newer without rewriting the whole thing. any assistance is appreciated










    import java.util.*;
    import java.io.*;

    public class OneLetterOff {
    public static void main(String[] args) throws Exception {

    DataInputStream input = new DataInputStream(System.in);
    System.out.print("1.Encrypt a file\t2.Decrypt a file\n");
    System.out.print("Enter your coice: ");
    int n = Integer.parseInt(input.readLine());
    System.out.print("Enter file name with out extension: ");
    String file = input.readLine();
    switch (n) {
    case 1:
    encrypt(file);
    break;
    case 2:
    decrypt(file);
    break;
    default:
    System.out.println("Invalid choice");
    }
    }

    public static void encrypt(String file) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(file + ".txt"));
    PrintWriter ps = new PrintWriter(new FileOutputStream("Encrypt.txt"));
    String line;
    while ((line = br.readLine()) != null) {
    for (int i = 0; i < line.length(); i++) {
    Character c = line.charAt(i);
    char ch = line.charAt(i);
    if (c.isLetter(ch)) {
    if (ch == 'z')
    ch = 'a';
    else if (ch == 'Z')
    ch = 'A';
    else
    ch++;
    }
    ps.write("" + ch);
    }
    }
    br.close();
    ps.close();
    System.out
    .println("file encrypted successfully check the Encrypt.txt file");
    }

    public static void decrypt(String file) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(file + ".txt"));
    PrintWriter ps = new PrintWriter(new FileOutputStream("Decrypt.txt"));
    String line;
    while ((line = br.readLine()) != null) {
    for (int i = 0; i < line.length(); i++) {
    Character c = line.charAt(i);
    char ch = line.charAt(i);
    if (c.isLetter(ch)) {
    if (ch == 'a')
    ch = 'z';
    else if (ch == 'A')
    ch = 'Z';
    else
    ch--;
    }
    ps.write("" + ch);
    }
    }
    br.close();
    ps.close();
    System.out
    .println("file decrypted successfully check the Decrypt.txt file");
    }

    }

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: I need a little help

    I have some depreciated code
    So which lines involve methods (or classes) that are depreciated? And do the API docs mention any alternative?

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a little help

    Quote Originally Posted by MrHatchi87 View Post
    I need a little help, I have some depreciated code, and I have no idea how to change it to newer without rewriting the whole thing. any assistance is appreciated










    import java.util.*;
    import java.io.*;

    public class OneLetterOff {
    public static void main(String[] args) throws Exception {

    DataInputStream input = new DataInputStream(System.in);
    System.out.print("1.Encrypt a file\t2.Decrypt a file\n");
    System.out.print("Enter your coice: ");
    int n = Integer.parseInt(input.readLine());
    System.out.print("Enter file name with out extension: ");
    String file = input.readLine();
    switch (n) {
    case 1:
    encrypt(file);
    break;
    case 2:
    decrypt(file);
    break;
    default:
    System.out.println("Invalid choice");
    }
    }

    public static void encrypt(String file) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(file + ".txt"));
    PrintWriter ps = new PrintWriter(new FileOutputStream("Encrypt.txt"));
    String line;
    while ((line = br.readLine()) != null) {
    for (int i = 0; i < line.length(); i++) {
    Character c = line.charAt(i);
    char ch = line.charAt(i);
    if (c.isLetter(ch)) {
    if (ch == 'z')
    ch = 'a';
    else if (ch == 'Z')
    ch = 'A';
    else
    ch++;
    }
    ps.write("" + ch);
    }
    }
    br.close();
    ps.close();
    System.out
    .println("file encrypted successfully check the Encrypt.txt file");
    }

    public static void decrypt(String file) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(file + ".txt"));
    PrintWriter ps = new PrintWriter(new FileOutputStream("Decrypt.txt"));
    String line;
    while ((line = br.readLine()) != null) {
    for (int i = 0; i < line.length(); i++) {
    Character c = line.charAt(i);
    char ch = line.charAt(i);
    if (c.isLetter(ch)) {
    if (ch == 'a')
    ch = 'z';
    else if (ch == 'A')
    ch = 'Z';
    else
    ch--;
    }
    ps.write("" + ch);
    }
    }
    br.close();
    ps.close();
    System.out
    .println("file decrypted successfully check the Decrypt.txt file");
    }

    }
    the bold items are the items that appear as depreciated, I don't recall what it recommends. Sorry, I'm at work now and can't access my laptop