Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

while loop problem in java

i'm having problems in the while loop near the end of the codes
which is in the switch case 3.

problems occur when i add in the nodes

the codes are like below



import java.lang.*;
import java.io.*;

public class MyPet
{
      private String name;
      private String status;
      private String ability;
      private MyPet next;

      public void setName(String w)
      {
            name=w;
      }

      public void setStatus(String x)
      {
            status=x;
      }

      public void setAbility(String y)
      {
            ability=y;
      }

      public void setNext(MyPet z)
      {
            next=z;
      }

      public String GetName()
      {
            return name;
      }
      
      public String GetStatus()
      {
            return status;
      }

      public String GetAbility()
      {
            return ability;
      }
      
      public MyPet GetNext()
      {
            return next;
      }


      
      
      public static void main(String args[])throws IOException
      {
      
      try
      {

      MyPet cat1 = new MyPet();
      MyPet cat2 = new MyPet();
      MyPet cat3 = new MyPet();
      MyPet cat4 = new MyPet();


      MyPet fish1 = new MyPet();
      MyPet fish2 = new MyPet();
      MyPet fish3 = new MyPet();
      MyPet fish4 = new MyPet();

      MyPet head1=cat1;
      MyPet head2=fish1;

      cat1.setName("Austin");
      cat1.setStatus("Hyperactive");
      cat1.setAbility("run in circles");
      cat1.setNext(cat2);

      cat2.setName("Chuckie");
      cat2.setStatus("Moody");
      cat2.setAbility("roll on grass");
      cat2.setNext(cat3);

      cat3.setName("Perry");
      cat3.setStatus("Healthy");
      cat3.setAbility("consume a lot of food");
      cat3.setNext(cat4);

      cat4.setName("Zoomy");
      cat4.setStatus("Healthy");
      cat4.setAbility("jumps 1 metre high");
      cat4.setNext(null);


      fish1.setName("Adorable");
      fish1.setStatus("Sick");
      fish1.setAbility("hiding behind rocks");
      fish1.setNext(fish2);

      fish2.setName("Goldie");
      fish2.setStatus("Healthy");
      fish2.setAbility("eats a lot");
      fish2.setNext(fish3);

      fish3.setName("Nemo");
      fish3.setStatus("Healthy");
      fish3.setAbility("hiding behind rocks");
      fish3.setNext(fish4);

      fish4.setName("Zelot");
      fish4.setStatus("Very Healthy");
      fish4.setAbility("Swim in circles");
      fish4.setNext(null);


      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

            int check_num = 0;
            
            while(check_num==0)
            {
            //copyright jk(c)
            
            System.out.println(" ");
            System.out.println("Welcome to my PET-STORE");
            System.out.println("=======================");
            System.out.println("(1) View the entire list of cats");
            System.out.println("(2) View the entire list of fishes");
            System.out.println("(3) Add new cat to your list");
            System.out.println("(4) Add new fish to your list");
            System.out.println("(5) Change the name of your cat");
            System.out.println("(6) Change the name of your fish");
            System.out.println("(7) See the ability of your cat");
            System.out.println("(8) See the ability of your fish");
            System.out.println("(9) Delete a dead cat");
            System.out.println("(10)Delete a dead fish");
            System.out.println(" ");
            System.out.println("Please select your option from (1-10):");
            System.out.println("__Press any other keys to exit__");

            String options = input.readLine();
            System.out.println(" ");
            System.out.println(" ");
      


            //change the input into an integer to be used in the switch case below

            int options_int = Integer.parseInt(options);
      
      
            switch(options_int)
            {

            //doing the listing of all cats
            case 1:
                  
                  {
                  System.out.println("List of Cats in My List");
                  System.out.println("=======================");
                  MyPet pointer = new MyPet();
                  pointer=head1;

                        while(pointer.GetNext() != null)
                        {
                        System.out.println("Name    :" +pointer.GetName());
                        System.out.println("Status  :" +pointer.GetStatus());
                        System.out.println("Ability :" +pointer.GetAbility());
                        System.out.println(" ");
                        pointer=pointer.GetNext();
                  }

                  System.out.println(" ");
                  System.out.println("Please press any keys.");
                  System.out.print("The screen will automatically ");
                  System.out.print("return to the main page!");
                  String any_keys = input.readLine();
                  System.out.println(" ");
                  check_num=0;
                  break;
                  }


                  
            //doing the listing of all fishes
            case 2:
                  
                  {
                  System.out.println("List of Fishes in My List");
                  System.out.println("=======================");
                  MyPet pointer = new MyPet();
                  pointer=head2;

                        while(pointer.GetNext() != null)
                        {
                        System.out.println("Name    :" +pointer.GetName());
                        System.out.println("Status  :" +pointer.GetStatus());
                        System.out.println("Ability :" +pointer.GetAbility());
                        System.out.println(" ");
                        pointer=pointer.GetNext();
                        }

                  System.out.println(" ");
                  System.out.println("Please press any keys.");
                  System.out.print("The screen will automatically ");
                  System.out.print("return to the main page!");
                  String any_keys = input.readLine();
                  System.out.println(" ");
                  check_num=0;
                  break;
                  }



            //doing the adding new cat part
            case 3:
                  {
                  System.out.println("Add New Cat to Your List");
                  System.out.println("========================");
                  MyPet previous = head1;
                  MyPet after = head1.GetNext();
                  MyPet NewNode = new MyPet();
                  System.out.println(" ");

                  System.out.println("Please enter a cat's name:");
                  String newCat = input.readLine();
                  NewNode.setName(newCat);
                  System.out.println(" ");

                  System.out.println("Please enter the status of the cat:");      
                  String newStatus = input.readLine();
                  NewNode.setStatus(newStatus);
                  System.out.println(" ");
            
                  System.out.println("Please enter the special ability of this cat:");
                  String newAbility = input.readLine();
                  NewNode.setAbility(newAbility);
                  System.out.println(" ");

                        while(previous.GetName().compareTo(NewNode.GetName())<0
                              && after.GetName().compareTo(NewNode.GetName())<0)
                        {
                              System.out.println("test");
                              previous = previous.GetNext();
                              after = after.GetNext();
                        }

                  previous.setNext(NewNode);
                  NewNode.setNext(after);


                  System.out.println(" ");
                  System.out.println("Your new cat in the list is "+NewNode.GetName());
                  System.out.println(" ");

                  System.out.println("Please press any keys.");
                  System.out.print("The screen will automatically ");
                  System.out.print("return to the main page!");
                  String any_keys = input.readLine();
                  System.out.println(" ");
                  check_num=0;
                  break;      
                  }
            
                  
                  




            default:

                  {
                  System.out.println("test");
                  check_num=1;
                  break;
                  }
                  
            }

            
            }

      
                  
      
      
            
      } catch(Exception e){ System.out.println("There's an error.Stay calm and enjoy :)"); }
      
      }

      
}

      

0
yuen_jk
Asked:
yuen_jk
  • 2
1 Solution
 
sgvillCommented:
Just glancing at it, it looks like you might get to a point where .GetNext is null.  you probably need to handle that a little more gracefully.  (i.e. if .GetNext is null, then you know that you need to add the cat to the end of the list).
0
 
makerpCommented:
you are doing a linked list - create a pointer to the last Pet in the list and then it is simple, for example

in main have

MyPet last_pet_in_list;

then when you add an entry simply;

last_pet_in_list.setNext(new_pet_entry);

and do not call setNext in your new_pet_entry
0
 
makerpCommented:
for the purpose of traversal you will also want to store the start pointer... so when creating a node it will look someting like this

MyPet first_pet;
MyPet last_pet;

if(first_pet == null){
  first_pet = new_pet;
  last_pet = new_pet;
}
else{
  last_pet.setNext(new_pet);
  last_pet = new_pet;
}
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now