Please! can anyone help me on this... Please!!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Please! can anyone help me on this... Please!!!

  1. #1
    Join Date
    Aug 2004
    Location
    Philippines
    Posts
    17

    Unhappy Please! can anyone help me on this... Please!!!

    I have here a String Code and I don't know what's the problem on this and If anyone can read this, Please! help me...
    And the Output of this is here and I don't know how the output will display...

    Output:

    Enter Any String: pretty (example string, you can enter any string)
    The length of the String is: 6
    The characters involved are:
    p is on index 0
    r is on index 1
    e is on index 2
    t is on index 3
    t is on index 4
    y is on index 5

    =======================================================

    The Code is Here:

    import java.io.*;
    class LengthName
    {
    public static void main(String[] AlcioAbaquita)throws IOException
    {
    String[] Names = ([" "]);
    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++)
    {
    if(name.equalsIgnoreCase(Names[ctr]))
    {
    System.out.println("The Length of String "+name+" is "+ctr);
    System.out.println("The Character "+name+" is on Index "+ctr);
    System.exit(0);
    }
    else
    {
    no=true;
    }
    }
    if(no==true)
    {
    System.out.println("Not Found!!!");
    }

    }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Question What ???

    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
    Last edited by sjalle; 09-07-2005 at 06:53 AM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center