This discussion is archived
7 Replies Latest reply: Jun 4, 2013 4:31 AM by TPD-Opitz-Consulting-com RSS

homework issue

1005446 Newbie
Currently Being Moderated
Hello guys, im here again with a little problem, well... for me its not so little. :))

Write a program, , that will prompt the user to enter a
number of names. Then display the sorted result.
For example, the program might be ran as follows:

Enter a name (q to quit)? John
Enter a name (q to quit)? Paul
Enter a name (q to quit)? Jim
Enter a name (q to quit)? q

Sorted results:
Jim
John
Paul


My problem is that i dont know how to make the loop stop from the users side(the program must strop when the user hits 'q'). Any hint is helpfull.
Now here my code:
import java.io.*;
public class TemaArrays
{
  static BufferedReader in;
  static int nameCount;
  static String fullname[];

  public static void bubbleSort(String data[], int size)
  {
    boolean done=false;
    int index;
    while(done==false)
    {done=true;
      for(index=1;index<size;index++)
      {
        if(data[index].compareTo(data[index-1])<0)
        {
          String temp= data[index];
          data[index]=data[index-1];
          data[index-1]= temp;
          done=false;
        }
      }
    }
  }
  
  public static void addName()
  {
    fullname[nameCount]=readInput();
    
    nameCount = nameCount+1;
    bubbleSort(fullname,nameCount);
  }
   public static String readInput()
   {
    try
    {
     String intrare=in.readLine();
     intrare = intrare.trim();
     return intrare;
    }
    catch(IOException e){}return "";
   }
           
  public static void main(String args[])
  {
      fullname= new String[20];
      nameCount=0;
      in=new BufferedReader(new InputStreamReader(System.in));
      boolean done = false;
      while(done==false)
      {
        System.out.println("Enter a name(q to quit)?");
        
        addName();
        
      //if(input.charAt(0)=='q')
         //for(int i=0;i<nameCount;i++){System.out.println(fullname);}
// done=true;

}



}

}

}
Edited by: 1002443 on Jun 4, 2013 4:06 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 1. Re: Arrays homework issue
    EJP Guru
    Currently Being Moderated
    When you drill down past all the irrelevant code and the irrelevant title, it turns out that your actual question has nothing to do with arrays whatsoever.
  • 2. Re: Arrays homework issue
    gimbal2 Guru
    Currently Being Moderated
    Check the commented out code and you may get an idea where the link to arrays used to be.

    Is it so hard? You know exactly what you want, but let me put it in easy to translate-to-code words:

    read input line from in
    if input equals "q", done is true


    And btw: use the New to Java forum, its there for a purpose.
  • 3. Re: Arrays homework issue
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    I'd move the line
    fullname[nameCount]=readInput();
    behind this
    System.out.println("Enter a name(q to quit)?");
    But I would assign the returned String to a local variable rather than to the array directly and pass this variable as parameter to <tt>addName();</tt> after checking it for beeing a 'q'.

    alternatively the check could be in <tt>addName();</tt>. <tt>addName();</tt> would return a boolean telling if the parameter was actually added as Name or not (you don't want to add the 'q' do you?). This woule lead to a nice bottom-controlled loop...

    bye
    TPD
  • 4. Re: Arrays homework issue
    aksarben Journeyer
    Currently Being Moderated
    Advice: Use a list, not an array.

    Arrays are inherently error prone & fragile. Java collections (in this case, lists) are your friend. They're easy & safe to use, & you never have array-out-of-bound errors. They also have build in sort capabilities (Collections.sort). Bubble sorts such you currently have, while easy to understand, are incredibly inefficient; once an array grows past a certain size, a bubble sort will be so slow you'll tear your hair out.
  • 5. Re: homework issue
    1005446 Newbie
    Currently Being Moderated
    This is what the code looks like now. Thx to all especially gimbal2
    import java.io.*;
    public class TemaArrays
    {
      static BufferedReader in;
      static int nameCount;
      static String fullname[];
    
      public static void bubbleSort(String data[], int size)
      {
        boolean done=false;
        int index;
        while(done==false)
        {done=true;
          for(index=1;index<size;index++)
          {
            if(data[index].compareTo(data[index-1])<0)
            {
              String temp= data[index];
              data[index]=data[index-1];
              data[index-1]= temp;
              done=false;
            }
          }
        }
      }
      
      public static void addName()
      {
        fullname[nameCount]=readInput();
        
        nameCount = nameCount+1;
        bubbleSort(fullname,nameCount);
      }
       public static String readInput()
       {
        try
        {
         String intrare=in.readLine();
         intrare = intrare.trim();
         return intrare;
        }
        catch(IOException e){}return "";
       }
               
      public static void main(String args[])
      {
          fullname= new String[20];
          nameCount=0;
          in=new BufferedReader(new InputStreamReader(System.in));
          boolean done = false;
         
          
          while(done==false)
          {
            System.out.println("Enter a name(q to quit)?");
             String name= readInput();
             
             
             if(name.equals("q"))
                     { done=true; }
             
             else{
               System.out.println("");    
               fullname[nameCount]=name;
               nameCount = nameCount+1;
               bubbleSort(fullname,nameCount);
             
             for(int i=0;i<nameCount;i++){System.out.println(fullname);}}
    }}}

    Edited by: 1002443 on Jun 4, 2013 4:11 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 6. Re: homework issue
    gimbal2 Guru
    Currently Being Moderated
    You may want to work a little on your code formatting, but good job. If you use Eclipse, you can:

    - press CTRL+SHIFT+F to auto-format code
    - in the preferences, under java -> editor -> save actions, you can configure to let Eclipse format code on save.

    If you use Netbeans, I'm sure the IDE has similar options. That way it is less of a chore.
  • 7. Re: homework issue
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    1002443 wrote:
         catch(IOException e){}return "";
    NEVER do this! Ignored exceptions will lead to obsessive debug sessions. The least thing you should do in the catch block is a call to <tt>e.printStackTrace()</tt> so that you'll see the exception in your console output.
          fullname= new String[20];
    You never check that the <tt>nameCount</tt> for beeing less then 20. This will lead to an <tt>ArrayIndexOutOfBoundsException</tt> (unless your teacher in a lasy one... ;o) )


    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points