Check my comments
Code:
import java.io.*;
class LengthName {
public static void main(String[] AlcioAbaquita) throws IOException {
String[] Names = ([" "]); // <--- syntax error
BufferedReader alcio = new BufferedReader(new InputStreamReader(System.in));
String name;
System.out.print("Enter String: ");
name = alcio.readLine();
int ctr;
boolean no = true;
for (ctr = 0; ctr < 0; ctr++) { // <--- loop will never start
// when the if-clause is entered the program exits, the no flag is
// completely useless.
if (name.equalsIgnoreCase(Names[ctr])) {
// ctr is the loop counter, it has no relevance to any string's length
System.out.println("The Length of String " + name + " is " + ctr);
// name is not a character,
System.out.println("The Character " + name + " is on Index " + ctr);
System.exit(0);
}
else {
no = true;
}
}
if (no == true) {
System.out.println("Not Found!!!"); // <--- what was not found...??
}
}
}
It seems to me like your intention is to make a small program that takes
a string input, writes out the length of the entered string and lists the
characters in the string together with their string index. The code above
does nothing of the sort....
This code does:
Code:
import java.io.*;
class LengthName {
public static void main(String[]args) throws IOException {
BufferedReader alcio = new BufferedReader(new InputStreamReader(System.in));
String name=null;
System.out.print("Enter String: ");
name = alcio.readLine();
name=name.trim(); // remove leading/trailing blanks
if (name.length()==0) {
System.out.println("That was a blank string");
System.exit(0);
}
// get string as an array of chars
char [] chrBuf=new char[name.length()];
name.getChars(0,name.length(),chrBuf,0);
System.out.println("The Length of String " + name + " is " + chrBuf.length);
for (int ctr = 0; ctr < chrBuf.length; ctr++) {
System.out.println("The Character " + chrBuf[ctr] + " is on Index " + ctr);
}
}
}
In fact, if it wasn't for your example output I would not have a clue what
the code you posted was trying to achieve
Bookmarks