java - How to take String as input using Scanner, when input is of the form(below)? -
java - How to take String as input using Scanner, when input is of the form(below)? -
input:
abc def feg cba this doing, of course of study wrong!
import java.util.scanner; public class p { public static void main(string []args){ scanner x = new scanner(system.in); int t = x.nextint(); (int j=0; j<t; j++) { string p[j] = x.nextline(); } (j=0; j<p.length(); j++) { (k=0; k<p.length(); k++) { if (p[j] = reverse(p[k])) { int q = p[k].length(); system.out.println(""+q+((q/2)+1)); } } } } }
the error message wrote because of code:
for (int j=0; j<t; j++) { string p[j] = x.nextline(); } if want create string array how it:
string[] p = new string[t]; it needs declared outside of for-loop. suggest this:
public static void main(string []args){ scanner x = new scanner(system.in); int t = x.nextint(); string[] p = new string[t]; (int j = 0; j < t; j++) { p[j] = x.nextline(); } (j=0; j<p.length(); j++) { ... } } the new code posted:
scanner x = new scanner(system.in); int t = x.nextint(); string p[] = new string[n]; (int j = 0; j < t; j++) { p[j] = x.nextline(); } (j = 0; j < p.length(); j++) { for(k = 1; k < p.length(); k++) { if (p[j] = reverse(p[k])) { int q = p[k].length(); system.out.println(""+q+((q/2)+1)); } with corrections i've commented code should this:
scanner x = new scanner(system.in); int t = x.nextint(); string[] p = new string[t]; (int j = 0; j < t; j++) { p[j] = x.nextline(); } (int j = 0; j < p.length(); j++) { (int k = 1; k < p.length(); k++) { if (p[j] = reverse(p[k])) { int q = p[k].length(); system.out.println("" + q + ((q / 2) + 1)); } } } java string
Comments
Post a Comment