java - System.out.print() is not displaying everything I expect and is unexpectedly printing new lines -
system.out.print()
not displaying in output box , when enter many characters automatically makes new line. entering 645 characters , when press enter should give me corresponding 215 characters. should using other system.out.print()
this?
import java.io.*; public class animal { public static void main(string[] args) { int = 0; int b = 1; int c = 2; system.out.print("codons="); string str = read(); (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("*"); } += 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); } }
it's unclear you're trying i'll try give pointers in hope solve problem.
- format code correctly. it's impossible read otherwise.
- use meaningful variables names.
a
,b
,c
, ,str
meaningless. people (yourself included) have easier time reading code if provide hints via clear variable , method names. - when find re-writing same lines of code on , on again (like massive list of conditionals) that's hint should restructure code. looks you're trying map groups of 3 characters single characters,
map<string, string>
can in 3 lines. - in vein, closely @ functionality provided
string
, particularlystring.substring()
- can each three-letter substring easily, without manually extracting each character. - avoid repeated function calls; if keep
str.charat()
behavior, can put them @ top of loop , assign them variables they're done once per iteration, each conditional checks variables.str.charat()
fast, many other methods aren't, , there's no need waste effort of calling same method on , on again. - there's no need directly read
system.in
's byte stream. can usescanner
or if necessarybufferedreader
. - you have
println
midway through conditional block, first"ggg"
case. intentional? if not, might why you're seeing unexpected new lines. otherwise, there's nothing in code generate undesirable new lines, it's possible terminal wrapping single line across multiple lines visually can see whole output. - you never use
write()
method; should delete that.
Comments
Post a Comment