String operation in Java -
is code correct? have mentioned doubts in form of comments in places:
public class pract1 { public static void main (string[] args) { int i; string [] array = new string[20]; // declaration correct? bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); system.out.println("enter array: "); array = br.readline(); // correct way accept input keyboard? i=0; while(array[i]!='\0') // can use null pointer concept in java? { system.out.println("the "+(i+1)+"character is:" +array[i]+"\n"); //want print each , every characters in string along position i++; } } }
this how it:
import java.io.bufferedreader; import java.io.inputstreamreader; public class pract1 { public static void main (string[] args) { string array = ""; //initialize empty string hold value keyboard bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); // initialize bufferedreader // while "stop" not typed on console, keep running while(!array.equals("stop")) { system.out.println("enter array: "); try { array = br.readline(); // read console system.out.println("array:" + array); for(int i=0; i<array.length(); i++) { //loop through input , show chars system.out.println("character "+(i+1)+" is: " +array.charat(i)); } } catch (exception e) { // catch exception , show message, not entire stacktrace system.out.println("error: " + e.getmessage()); } system.out.println(""); } } }
Comments
Post a Comment