0

My program stops after the first element gets into array. Could you help me? Here's the code: sorry for that i've made some changes now ArrayQueue is my class where all my methods are implemented QueueElement is another class that holds the type of element.. i think the problem now is at the ArrayQueue class

              import java.io.*;
             import java.util.Scanner;
           public class QueueTest
          { private static  Scanner scan =new Scanner(System.in);
               public static void main(String[] args)
                   {
              ArrayQueue queue = new ArrayQueue();
              System.out.println("insert :" +
            " P: to print the array "+
            "S: to sort "+
            "E: to empty"); 
            do
             {
        if (userInput.startsWith("Y")) {
            System.out.println("insert the value of element");
            int vl = scan.nextInt();
            System.out.println("insert the description");
            String p = scan.next();
            System.out.println("insert true or false");
            boolean vlf = scan.nextBoolean();
            queue.shtoElement(new QueueElement(vl, p, vlf));
        } else if (userInput.startsWith("P")) {
            queue.shfaqArray();//display the array
        } else if (userInput.startsWith("S")) {
            queue.rendit();//sort the array
        } else if (userInput.startsWith("E")) {
            queue.Empty();//empty array
        } 
        System.out.println("Insert:" + " P: to display the array "
                + " S: to sort array "
                + " E: to empty");
        userInput = scan.next();
        } while (!userInput.equalsIgnoreCase("exit"));
              }
                 }

here is the class ArrayQueue

                 public class ArrayQueue implements Queue

                             {
                //build the queue
          QueueElement[] tabela= new QueueElement[MADHESIA];
            QueueElement element= new QueueElement();
          //test if queue is empty
             public boolean isEmpty()
                {

               boolean empty = true;
                 for(int k =0;k<tabela.length;k++)
                      {
                            if(tabela[k] !=null)
                        empty =false;
                   break;
                                }
                            return empty;
                                          }

            public boolean isFull()
                {
                    boolean full=false;
                    for(int k=0;k<tabela.length;k++)
            {
        if(tabela[k]!=null)
            full=true;
        break;
            }
    return full;
        }

    //sort the array
        public void rendit()
           { 
    QueueElement temp=tabela[0];
    for(int i=0;i<tabela.length;i++)
    {
        for(int j=i+1;j<tabela.length;j++)
            if(tabela[j]!=null)
            {
            if(tabela[j].compareTo(tabela[i])==1)
            {
                tabela[j]=temp;
                tabela[i]=tabela[j];
                temp=tabela[i];
            }
            else 
                {
                nrElementeve++;
                }
            }
        if(tabela[i].getVlefshmeria()==false)
        {
            hiqElement(i,temp);
             }
            }
               }
    // add element into the array

           public void shtoElement(QueueElement el)
                  {     
                if(isEmpty())
        {
        tabela[0]=el;
        }
    else
         {
            for(int i=0;i<tabela.length;i++) 
               {    
                   if(tabela[i]!= null && (el.compareTo(tabela[i])==0))
                       {
    System.out.println("element can't be added into array cause it exists !");
                        }              
                   {
                       if(isFull())
                       {                        
                            int index=tabela.length;
                            tabela=rritMadhesine(tabela);
                            tabela[index]=el;
                     }  
                 else
                 {
            for(int j=0;j<tabela.length;j++)
        {
        if(tabela[j]==null)
                  tabela[j]=el;
        break;
        }
                        }
               }
         }
               }

             } 
       //find max of array

         public QueueElement gjejMax()
            {
          QueueElement max = tabela[0];
           for (QueueElement element :tabela)
              {
    if(element.getValue() > max.getValue())
        {
        max =element;
        }
    return max;
           }
          return max;
                } 
            //find min of array
            public QueueElement gjejMin()
                  {
                    QueueElement min= tabela[0];
                     for(QueueElement element : tabela)
                  {
                  if(element.getValue()< min.getValue())
               {
                    min=element;
                           }
                        return min;
                              }
                              return min;
                                }

             //remove element from array

             public void hiqElement(int indeksi, QueueElement temp) 
                   {
                      if(tabela.length > 0)
                      {
                  if(gjejMax().compareTo(temp)==0 || gjejMin().compareTo(temp)==0)
               {
                   System.out.println("element can't be removed!");
                   }
                     else
            {
                  for(int i=indeksi;i<tabela.length;i++)
                        {
                      tabela[i]=tabela[i+1];
                               }
                    }
                          nrElementeve--;
                                 }
                          }

        //empty array

           public void Empty()
                {
            tabela= new QueueElement[MADHESIA];
                           }

                       //display the array

                  public  void shfaqArray()
                     {
              for(int i=0;i< tabela.length;i++)
               {
              if(tabela[i]!=null)
             {
                System.out.println(tabela[i]);
            }
                else
      break;
                  }
                }

         //increase the length of array

         public static QueueElement [] rritMadhesine(QueueElement[] array){  
            QueueElement[] tab=new QueueElement[array.length+2];  
              return tab; 
                    }
                     private int nrElementeve;
                 private static final int MADHESIA = 10;
                      }
7
  • 5
    More ?s means more answers, right.. Are you getting any errors? Commented Jul 30, 2012 at 14:28
  • 3
    Atleast format it properly , a large no of errors also occur since you can miss out the {} in badly formatted code. Commented Jul 30, 2012 at 14:28
  • 2
    And whats the long commented part in the code ? Remove it if not needed before posting here. Commented Jul 30, 2012 at 14:31
  • and what's ArrayQueue, its uncompilable code Commented Jul 30, 2012 at 14:33
  • 1
    please provide SSCCE(Short, Self Contained, Correct Example) Commented Jul 30, 2012 at 14:34

1 Answer 1

2

Don't know what ArrayQueue and QueueElement are, but...

It seems like you're reading user input then going through a decision tree to decide what to do based on that input. But at each point, you are re-reading the user input:

     if(scan.next().startsWith("Y"))
     {
        System.out.println("Jepni vleren e elem");
        vl=scan.nextInt();
        System.out.println("Jepni pershkrimin  e elem");
        p=scan.next();
        System.out.println("Jepni vlefshmerine e elem");
        vlf=scan.nextBoolean();
        queue.shtoElement(new QueueElement(vl,p,vlf));
     }
     else
     {
        if(scan.next().startsWith("P"))
           queue.shfaqArray();
     }
     if(scan.next().startsWith("E"))
        queue.Empty();
     if(scan.next().startsWith("S"))
        queue.rendit();
     else
     {
        break;
     }

I think you want something more like:

     String userInput = scan.next();
     if(userInput.startsWith("Y"))
     {
        System.out.println("Jepni vleren e elem");
        vl=scan.nextInt();
        System.out.println("Jepni pershkrimin  e elem");
        p=scan.next();
        System.out.println("Jepni vlefshmerine e elem");
        vlf=scan.nextBoolean();
        queue.shtoElement(new QueueElement(vl,p,vlf));
     }
     else if(userInput.startsWith("P"))
        queue.shfaqArray();
     else if(userInput.startsWith("E"))
        queue.Empty();
     else if(userInput.startsWith("S"))
        queue.rendit();
     else
     {
        break;
     }

Where you read the user input once (String userInput = scan.next();) and then decide what to do based on the userInput variable and not re-scan each time. Also, it looks like you might be missing a couple of else statements somewhere in the middle there.

Sign up to request clarification or add additional context in comments.

11 Comments

the problem with my code is that only the first element gets in array, than the prog do nothing,,, i want the prog to continue running until the user input 'q' , so i want to display " put the value of elem, put the descript of elem.. and if it is true or false" and these input gets in the array
@AnnckyHome probably because it either breaks out of the loop or it's waiting for more input because you are calling scan.next() at every if statement, did you read my answer?
yes.. i read.. so i don't have to put every time .next() method ??? ok i'll try thanx
still the same.. cause it gets the first elem i put and than the prog terminates??? any other idea????
@AnnckyHome Not really, I'm looking at the block of your code that I've highlighted above, which I modified, but I see no modifications in the code in your question. Can you modify the code so as to reflect what you've changed when you said "so i don't have to put every time .next() method ??? ok i'll try thanx"?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.