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: Java Program String Arrays Problem

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Program String Arrays Problem

    The program allows the user to enter a phrase, and the program is supposed to print the phrase using ROT13 , which replaces each english letter with the letter 13 places forward, using arrays to store the characters. This is what I have so far but it doesn't work. When I run the code, after you enter a phrase, It will output letters starting with "O" and keep going (O P Q R...) for how many characters long the phrase was. Why is this happening?

    import java.io.*;
     
    public class program
    {
     
    public static void main (String [] args) throws IOException
    {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed 
     
     String key [] = {"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 keyA [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};
    String letter;
     
    System.out.println("Enter a phrase:");
    String phrase = myInput.readLine();
    int y = 0, i = 0;
    while ( y <= phrase.length()){
     
    letter = Character.toString(phrase.charAt(y));
     
    while(i <= y){
     
    if (letter != key[i]){
      keyA [i] = keyA[i];
    }
    i++;
    }
    System.out.println(keyA [i]);
    y++;
    } 
    }
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Program String Arrays Problem

    Honestly, it was too convoluted for me to figure out why it was doing what it was doing. Right off I saw you were trying to make while() loops do for() loop work. Also that assignment of keyA[i] to itself was pointless and when I removed that, it all became kind of pointless.

    If you want to iterate through one thing relative to another, you need nested for() loops. For instance, if I have int arrays x and y, and I want to increment z every time x is equal to y (of course this is not runnable code):

    int[] x = new int[10];
    int[] y = new int[10];
    int z;
     
    for(int i = 0; i < x.length; i++) {
     
         for(int j = 0; j < y.length; j++) {
     
            if(x[i] == y[j]) {
               z++;
               break;
               }
             }
         }

    i starts at 0, first index of x. j runs through the length of y until it finds that the digit being checked up on in the x array is equal to the target digit in the y array. Then it increments z and breaks. Back in the i-loop, i increments, and the process repeats, checking the next digit of x.

    You are using while() loops trying to do this job. I suggest you get rid of them and replace with nested for()'s. They are much easier to understand, mostly because they are a standard.

    -summit45

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Program String Arrays Problem

    @incxx "It will output letters starting with "O" and keep going (O P Q R...) for how many characters long the phrase was. Why is this happening?" Step through the code to see what is happening. Sounds like there is a problem with the loops. Follow through and see why it does what it does

    @summit45 Please read The problem with spoonfeeding

Similar Threads

  1. [SOLVED] Hangman Program Java: Comparing the User's Guess with Another String Letter by Letter
    By theonlydvr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 29th, 2013, 05:35 PM
  2. Problem with java program and CPU usage
    By Bahramudin in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2012, 09:26 AM
  3. String Arrays []
    By djl1990 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 11th, 2012, 08:37 PM
  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: 5
    Last Post: January 30th, 2009, 09:31 PM