-1

System.out.print() is not displaying everything in the output box and when I enter to many characters it automatically makes a new line. I am entering 645 characters and when I press enter it should give me the corresponding 215 characters. Should i be using something other than System.out.print() for this?

import java.io.*;

public class Animal {
  public static void main(String[] args) {
    int A = 0;
    int B = 1;
    int C = 2;
    System.out.print("codons=");
    String str = read();

    for (int num = 0; num <= str.length() / 3; num++) {
      if (str.charAt(A) == 'T' && str.charAt(B) == 'A' && str.charAt(C) == 'G') {
        System.out.print("a");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'G' && str.charAt(C) == 'T') {
        System.out.print("b");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'T' && str.charAt(C) == 'T') {
        System.out.print("c");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'T' && str.charAt(C) == 'T') {
        System.out.print("d");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'A' && str.charAt(C) == 'A') {
        System.out.print("e");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'G' && str.charAt(C) == 'C') {
        System.out.print("f");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'A' && str.charAt(C) == 'C') {
        System.out.print("g");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'C' && str.charAt(C) == 'A') {
        System.out.print("h");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'T' && str.charAt(C) == 'G') {
        System.out.print("i");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'T' && str.charAt(C) == 'T') {
        System.out.print("j");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'C' && str.charAt(C) == 'A') {
        System.out.print("k");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'A' && str.charAt(C) == 'C') {
        System.out.print("l");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'A' && str.charAt(C) == 'A') {
        System.out.print("m");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'G' && str.charAt(C) == 'C') {
        System.out.print("n");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'G' && str.charAt(C) == 'T') {
        System.out.print("o");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'C' && str.charAt(C) == 'A') {
        System.out.print("p");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'T' && str.charAt(C) == 'A') {
        System.out.print("q");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'T' && str.charAt(C) == 'A') {
        System.out.print("r");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'C' && str.charAt(C) == 'T') {
        System.out.print("s");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'G' && str.charAt(C) == 'A') {
        System.out.print("t");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'C' && str.charAt(C) == 'C') {
        System.out.print("u");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'T' && str.charAt(C) == 'G') {
        System.out.print("v");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'T' && str.charAt(C) == 'C') {
        System.out.print("w");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'G' && str.charAt(C) == 'T') {
        System.out.print("x");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'A' && str.charAt(C) == 'T') {
        System.out.print("y");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'G' && str.charAt(C) == 'G') {
        System.out.print("z");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'C' && str.charAt(C) == 'T') {
        System.out.print("0");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'T' && str.charAt(C) == 'T') {
        System.out.print("1");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'C' && str.charAt(C) == 'T') {
        System.out.print("2");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'A' && str.charAt(C) == 'T') {
        System.out.print("3");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'G' && str.charAt(C) == 'A') {
        System.out.print("4");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'C' && str.charAt(C) == 'G') {
        System.out.print("5");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'C' && str.charAt(C) == 'C') {
        System.out.print("6");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'A' && str.charAt(C) == 'T') {
        System.out.print("7");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'G' && str.charAt(C) == 'C') {
        System.out.print("8");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'T' && str.charAt(C) == 'A') {
        System.out.print("9");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'T' && str.charAt(C) == 'A') {
        System.out.print(" ");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'G' && str.charAt(C) == 'G') {
        System.out.println("");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'G' && str.charAt(C) == 'C') {
        System.out.print(">");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'G' && str.charAt(C) == 'G') {
        System.out.print("<");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'C' && str.charAt(C) == 'T') {
        System.out.print("+");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'A' && str.charAt(C) == 'G') {
        System.out.print("}");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'A' && str.charAt(C) == 'C') {
        System.out.print("/");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'C' && str.charAt(C) == 'A') {
        System.out.print("=");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'G' && str.charAt(C) == 'A') {
        System.out.print(".");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'A' && str.charAt(C) == 'G') {
        System.out.print("!");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'A' && str.charAt(C) == 'G') {
        System.out.print(":");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'G' && str.charAt(C) == 'A') {
        System.out.print("'");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'T' && str.charAt(C) == 'G') {
        System.out.print(",");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'C' && str.charAt(C) == 'G') {
        System.out.print("@");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'C' && str.charAt(C) == 'C') {
        System.out.print("-");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'G' && str.charAt(C) == 'G') {
        System.out.print("newline");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'T' && str.charAt(C) == 'C') {
        System.out.print("\\");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'A' && str.charAt(C) == 'A') {
        System.out.print("{");
      } else if (str.charAt(A) == 'G' && str.charAt(B) == 'A' && str.charAt(C) == 'C') {
        System.out.print("(");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'C' && str.charAt(C) == 'C') {
        System.out.print("&");
      } else if (str.charAt(A) == 'C' && str.charAt(B) == 'C' && str.charAt(C) == 'G') {
        System.out.print(")");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'T' && str.charAt(C) == 'C') {
        System.out.print("$");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'G' && str.charAt(C) == 'G') {
        System.out.print("]");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'T' && str.charAt(C) == 'C') {
        System.out.print("#");
      } else if (str.charAt(A) == 'T' && str.charAt(B) == 'G' && str.charAt(C) == 'T') {
        System.out.print(";");
      } else if (str.charAt(A) == 'A' && str.charAt(B) == 'T' && str.charAt(C) == 'G') {
        System.out.print("*");
      }
      A += 3;
      B += 3;
      C += 3;
    }
    System.out.println("");
  }

  public static String read() {
    byte[] buffer = new byte[647];
    try {
      int numBytes = System.in.read(buffer);
    } catch (IOException e) {
      System.out.print("Error: " + e);
      System.exit(1);
    }
    String str = new String(buffer);
    return (str);
  }

  public static void write(String str) {
    System.out.print(str);
  }
}
dimo414
  • 42,340
  • 17
  • 131
  • 218
Ineedhelp
  • 11
  • 4

2 Answers2

1

It's unclear what you're trying to do but I'll try to give you some pointers in the hope they will help solve your problem.

  • Format your code correctly. It's impossible to read otherwise.
  • Use meaningful variables names. A, B, C, and str are all meaningless. People (yourself included) will have an easier time reading your code if you provide hints via clear variable and method names.
  • When you find yourself re-writing the same lines of code over and over again (like your massive list of conditionals) that's a good hint you should restructure your code. It looks like you're trying to map groups of three characters to single characters, a Map<String, String> can do this easily in roughly three lines.
  • In that vein, look closely at the functionality provided by String, particularly String.substring() - you can get each three-letter substring easily, without manually extracting each character.
  • Avoid repeated function calls; even if you keep your str.charAt() behavior, you can put them at the top of your for loop and assign them to variables so they're only done once per iteration, then each conditional simply checks the variables. str.charAt() is fast, but many other methods aren't, and there's no need to waste the effort of calling the same method over and over again.
  • There's no need to directly read from System.in's byte stream. You can simply use a Scanner or if necessary a BufferedReader.
  • You have a println midway through your conditional block, for the first "GGG" case. Is that intentional? If not, it might be why you're seeing unexpected new lines. Otherwise, there's nothing in your code that will generate undesirable new lines, it's possible your terminal is simply wrapping the single line across multiple lines visually so you can see the whole output.
  • You never use your write() method; you should probably just delete that.
Community
  • 1
  • 1
dimo414
  • 42,340
  • 17
  • 131
  • 218
0

This will give you 215 characters, only if in your input string each time any if condition satisfied. Otherwise it will not be able to print anything. if you still think, that your input string satisfy the conditions and you are not getting output as expected then share your input for this program.

One more correction to your question, for the below condition, you are using System.out.println whereas for others it is System.out.print, if this condition get's satisfied then you will get the other characters in new line.

   else if (str.charAt(A) == 'G' && str.charAt(B) == 'G'
                && str.charAt(C) == 'G') {
            System.out.println("");
        }
Vicky
  • 706
  • 6
  • 16