?
Solved

while loop problem in java

Posted on 2005-03-17
5
Medium Priority
?
356 Views
Last Modified: 2010-04-17
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
Comment
Question by:yuen_jk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 4

Expert Comment

by:sgvill
ID: 13571568
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
 
LVL 10

Expert Comment

by:makerp
ID: 13572360
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
 
LVL 10

Accepted Solution

by:
makerp earned 500 total points
ID: 13572370
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question