Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can you help me?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can you help me?

    import javax.swing.*;
       import javax.swing.JOptionPane;
       import javax.swing.UIManager;
       import javax.swing.plaf.ColorUIResource;
     
        public class ivan3
       {
           public static void main(String[] args)
          { 
             UIManager uim=new UIManager();  															 
             uim.put("OptionPane.background",new ColorUIResource(0,0,190));					
             uim.put("Panel.background",new ColorUIResource(0,0,190));				
             uim.put("OptionPane.messageForeground",new ColorUIResource(255,255,255));
             uim.put("Panel.messageForeground",new ColorUIResource(255,255,255));
    	final String HEllo =       "SORTING AND SEARCHING NAMES!";
    int ctr = 0, ctr1 = 0, ctr2, ctr3 = 0;
    int search;
    int num = 0, cc = 0;
    String temp = "";
    String searchee = "";
     String JILL = "";
     String range = "[A-Z a-z]*"; 
    JOptionPane.showMessageDialog(null, HEllo,null,JOptionPane.PLAIN_MESSAGE);
    while (cc != 1){
    try{
    JILL = JOptionPane.showInputDialog(null, 
    "Please put a number of names you want to sorted: ");
    num = Integer.parseInt(JILL);
    }
    catch(NumberFormatException k){
    JOptionPane.showMessageDialog(null, "Numbers only, not Letters or Symbols");
    cc--;
    }
    cc++;
    }
    String name[] = new String[num];
    for (int i = 0; i < name.length; i++){
    JILL= JOptionPane.showInputDialog("Enter the name: ");
    name[i] = JILL;
    char letter = name[i].charAt(0); // charAt() is method to find character at index number and return a single character from whole string.
    if (!name[i].matches(range)){
    JOptionPane.showMessageDialog(null, "Input a Letters only.");
    i--;
    }
    else if (Character.isLowerCase(letter)){ // this code method determines whether the specified char value is lowercase.
    JOptionPane.showMessageDialog(null, 
    " Capitalization of the first letter is required.");
    i--;
    }
    }
    System.out.println("Your unsorted names: ");
    for (int f = 0; f < name.length; f++){
    System.out.println (name[f]);
    }
    while (ctr1 < name.length-1){
    ctr2 = ctr1 + 1;
    while (ctr2 < name.length){
    if (name[ctr1].compareTo(name[ctr2])>0){
    temp = name[ctr1];
    name[ctr1] = name[ctr2];
    name[ctr2] = temp;
    }
    ctr2++;
    }
    ctr1++;
    }
    System.out.println("Your Sorted names: ");
    for (int e = 0; e < name.length; e++){
    System.out.println (name[e]);
    }
     
    for (int v = 0; v != 1; v++){
    JILL = JOptionPane.showInputDialog(null, 
    "Enter the name you want to search or find.");
    searchee = JILL;
    char t = searchee.charAt(0);
    if (!searchee.matches(range)){
    JOptionPane.showMessageDialog(null, "Letters only.");
    v--;
    }
    else if (Character.isLowerCase(t)){
    JOptionPane.showMessageDialog(null, 
    "Capitalization of the first letter is required..");
    v--;
    }
    }
    while (ctr3 != name.length){
    search = ctr3;
    if (searchee.equals(name[search])){
    JOptionPane.showMessageDialog(null, "Name found SUCCESFULLY!.");
    break;
    }
    search++;
    ctr3++;
    while (true){JOptionPane.showMessageDialog(null, "Name not found.");
     
    for (int v = 0; v != 1; v++){
    JILL = JOptionPane.showInputDialog(null, 
    "Enter the name 'AGAIN' you want to search or find.");
    searchee = JILL;
    char t = searchee.charAt(0);
    if (!searchee.matches(range)){
    JOptionPane.showMessageDialog(null, "Letters only.");
    v--;
    }
    else if (Character.isLowerCase(t)){
    JOptionPane.showMessageDialog(null, 
    "Capitalization of the first letter is required..");
    v--;
    }
    if (searchee.equals(name[search])){
    JOptionPane.showMessageDialog(null, "Name found SUCCESFULLY!.");
    break;
    }
    }
    }
    }
    }
          }

    //this program is about sorting and searching a name anyone can help me fix this things?
    Last edited by Norm; January 27th, 2013 at 07:19 AM. Reason: added ending code tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can you help me?

    Please explain the problems are you are having. Copy and paste here the program's output and explain what is wrong with it.

    You need to edit the post and properly format the code by adding indentations for nested statements. Unformated code is hard to read and understand. There are too many statements starting in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can you help me?

    my problem to that code is the looping (sorry to my grammar)
    i want to do to that code is if the name is founded the program will stop, and if the name is not found the program will looping again to the question "enter the name you want to search or find until they found. can you try that codes in your JCREATOR?

    --- Update ---

    Quote Originally Posted by Norm View Post
    Please explain the problems are you are having. Copy and paste here the program's output and explain what is wrong with it.

    You need to edit the post and properly format the code by adding indentations for nested statements. Unformated code is hard to read and understand. There are too many statements starting in the first column.

    Your unsorted names:
    Ivan
    Ass
    Your Sorted names:
    Ass
    Ivan
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:695)
    at ivan3.main(ivan3.java:101)

    Process completed.

    that is the out put

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can you help me?

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:695)
    at ivan3.main(ivan3.java:101)
    The code tries to get the first character (index=0) of a String at line 101 that is empty.
    Check the code at line 101 to see why it is trying to look at characters in an empty String.
    It could use the String class's length() method to see if the String is empty.

    You need to edit the post and properly format the code by adding indentations for nested statements. Unformated code is hard to read and understand. There are too many statements starting in the first column.
    If you don't understand my answer, don't ignore it, ask a question.