Question

Hey guys ! Need help ! Can not get my code to run from my nodelist....

Hey guys ! Need help ! Can not get my code to run from my nodelist. When i run it referencing my array it works, however when I try to reference my nodelist, it does not work please help!:

package zipcode;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class ZipCodeDriver {

public static void main(String[] args) {
//file contening the data that will be loaded into the array
String filename = "zipcodes.txt";

// Load data
try {
Scanner infile = new Scanner(new File(filename));
ZipCodeList1 myZipCodelist = new ZipCodeList1();
ZipCode c;
while(infile.hasNext()){
c = new ZipCode();
c.setZipcode(infile.nextLine());
c.setTown(infile.nextLine());
c.setState(infile.nextLine());
myZipCodelist.add(c);
}
infile.close();
System.out.println("Loaded " + myZipCodelist.getSize() + " zip codes");
  
// Search for the data
String search = "";
Scanner keyboard = new Scanner(System.in);
while(true){
System.out.print("Hello, please enter zip code to search (type done to stop): ");
search = keyboard.next();
if(search.equalsIgnoreCase("done"))
break;
c = myZipCodelist.find(search);
if(c == null)
System.out.println("Sorry but the zip code entered was not found");
else
System.out.println("Found " + search + ": [" + c + "]");
}
  
// In case we don''t find the data
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}

}

--

package zipcode;


public class ZipCode {

//add components
private String zipcode;
private String town;
private String state;

//add constructors
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
  
public String toString(){
return "ZipCode: " + zipcode + ", Town: " + town + ", State: " + state;
}
  
}
--

package zipcode;


public class ZipCodeList {
// Add components; The limit of the size, the array holding the data and a counter
private static int MAX_LIST_SIZE = 1000;
private ZipCode[] zipcodes;
private int count;
//counter
public ZipCodeList(){
zipcodes = new ZipCode[MAX_LIST_SIZE];
count = 0;
}
//Limit of the array
public void add(ZipCode c){
if(count == MAX_LIST_SIZE){
System.out.println("Could not add " + c);
}
else{
zipcodes[count++] = c;
}
}
//find the zipcode
public ZipCode find(String code){
for(int i = 0; i < count; i++){
if(zipcodes[i].getZipcode().equals(code))
return zipcodes[i];
}
// not found
return null;
}
  
//find size
public int getSize(){
return count;
}
}

--

import zipcode.ZipCode;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author camil
*/
public class ZipCodeListNode {
public ZipCode data;
public ZipCodeListNode next;
public ZipCodeListNode(ZipCode data, ZipCodeListNode next) {
this.data = data;
this.next = next;
}
public ZipCodeListNode(ZipCode data) {
this.data = data;
}
}
class ZipCodeList1 {
private int count;
private ZipCodeListNode head;

public ZipCodeList1() {
head = null;
count = 0;
}

public void add(ZipCode c) {
head = new ZipCodeListNode(c, head);
count++;
}

public ZipCode find(String code) {
ZipCodeListNode start = head;
while(start != null) {
if(start.data.getZipcode().equals(code)) {
return start.data;
}
start = start.next;
}

return null; // not found
}

public int getSize() {
return count;
}
}

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package zipcode;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ZipCodeDriver {

   public static void main(String[] args) {
       // file contening the data that will be loaded into the array
       String filename = "zipcodes.txt";
       System.out.println(filename);

       // Load data
       try {
           Scanner infile = new Scanner(new File(filename));
          
           /*Create new object myZipCodeList of type ZipCodeList1,
           * copy the Zipcode object c which was created and assigned with zipcode,town,state.
           and use the add method of class ZipCodeList1 and add ZipCode c to the ZipCodeList1 myZipCodeList.
          
           Rest of things are implemented earlier.*/
          
            ZipCodeList1 myZipCodeList = new ZipCodeList1();
           ZipCode c;
           while (infile.hasNext()) {
               c = new ZipCode();
               c.setZipcode(infile.nextLine());
               c.setTown(infile.nextLine());
               c.setState(infile.nextLine());
                myZipCodeList.add(c);
           }
           infile.close();
           System.out.println("Loaded " + myZipCodeList.getSize() + " zip codes");
          
           // Search for the data
           String search = "";
           Scanner keyboard = new Scanner(System.in);
           while (true) {
               System.out.print("Hello, please enter zip code to search (type done to stop): ");
               search = keyboard.next();
               if (search.equalsIgnoreCase("done"))
                   break;
                c = myZipCodeList.find(search);
               if (c == null)
                   System.out.println("Sorry but the zip code entered was not found");
               else
                   System.out.println("Found " + search + ": [" + c + "]");
           }

           // In case we don''t find the data
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
   }

}

ZipCode Class:

package zipcode;

public class ZipCode {

   // add components
   private String zipcode;
   private String town;
   private String state;

   // add constructors
   public String getZipcode() {
       return zipcode;
   }

   public void setZipcode(String zipcode) {
       this.zipcode = zipcode;
   }

   public String getTown() {
       return town;
   }

   public void setTown(String town) {
       this.town = town;
   }

   public String getState() {
       return state;
   }

   public void setState(String state) {
       this.state = state;
   }

   public String toString() {
       return "ZipCode: " + zipcode + ", Town: " + town + ", State: " + state;
   }

}

ZipCodeListNode Class:

package zipcode;

public class ZipCodeListNode {
   public ZipCode data;
   public ZipCodeListNode next;

   public ZipCodeListNode() {

   }

   public ZipCodeListNode(ZipCode data) {
       this.data = data;
   }

   public ZipCodeListNode(ZipCode data, ZipCodeListNode next) {
       this.data = data;
       this.next = next;
   }

}

class ZipCodeList1 {
   private int count;
   private ZipCodeListNode head;

   public ZipCodeList1() {
       head = null;
       count = 0;
   }

   public void add(ZipCode c) {
       head = new ZipCodeListNode(c, head);
       count++;
   }

   public ZipCode find(String code) {
       ZipCodeListNode start = head;
       while (start != null) {
           if (start.data.getZipcode().equals(code)) {
               return start.data;
           }
           start = start.next;
       }

       return null; // not found
   }

   public int getSize() {
       return count;
   }
  
   public ZipCode getZipcode(){
       return this.getZipcode();
   }
  
  
}

ZipCodeList Class:

package zipcode;

public class ZipCodeList {
   // Add components; The limit of the size, the array holding the data and a
   // counter
   private static int MAX_LIST_SIZE = 1000;
   private ZipCode[] zipcodes;
   private int count;

   // counter
   public ZipCodeList() {
       zipcodes = new ZipCode[MAX_LIST_SIZE];
       count = 0;
   }

   // Limit of the array
   public void add(ZipCode c) {
       if (count == MAX_LIST_SIZE) {
           System.out.println("Could not add " + c);
       } else {
           zipcodes[count++] = c;
       }
   }

   // find the zipcode
   public ZipCode find(String code) {
       for (int i = 0; i < count; i++) {
           if (zipcodes[i].getZipcode().equals(code))
               return zipcodes[i];
       }
       // not found
       return null;
   }

   // find size
   public int getSize() {
       return count;
   }
}

Follow the image for the changes made in ZipCodeDriver

Compare the changes made(left side red color) and previous(right side green color)

Sample input:

560036
Chennai
Tamil Nadu
560034
Bangalore
Karnataka

Output:
Loaded 2 zip codes
Hello, please enter zip code to search (type done to stop): 560036
Found 560036: [ZipCode: 560036, Town: Chennai, State: Tamil Nadu]
Hello, please enter zip code to search (type done to stop): 560034
Found 560034: [ZipCode: 560034, Town: Bangalore, State: Karnataka]
Hello, please enter zip code to search (type done to stop): 10001
Sorry but the zip code entered was not found
Hello, please enter zip code to search (type done to stop): done

Add a comment
Know the answer?
Add Answer to:
Hey guys ! Need help ! Can not get my code to run from my nodelist....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT