java - Need help creating a program that finds lightest and heaviest dog -
i have huge amount of trouble java project. have write class keeps track of name, breed, date, , weight of dog must input file containing 1 line per dog. need accessor,modifier, arraylist , tostring method. main program needs determine lightest dog , heaviest dog.
import java.io.*; import java.util.*; import java.util.arraylist; public class kennel { public static void main(string args[]) { string line = ""; // string var hold entire line if (args.length < 1) { system.out.println("\n forgot put file name on command line."); system.exit(1); }; string infile = args[0]; // file name off command line scanner sc = null; try { sc = new scanner(new file(infile)); } catch (exception e) { system.out.println("file not found"); system.exit(1); } // print message explaining purpose of program. system.out.println("\nthis program inputs file "); system.out.println(args[0]); system.out.println("and outputs information dogs in registry."); system.out.println("\nwritten phil trout."); system.out.println(); system.out.println("property "+"name "+"value "); system.out.println(); // loop //double heaviest = 0.0; //double lightest = 1000.0; while(sc.hasnextline()) { // read line input file via sc line line = sc.nextline(); } { //got email infromation stringtokenizer stk = new stringtokenizer(line); string name = stk.nexttoken(); string breed = stk.nexttoken(); int month = integer.parseint(stk.nexttoken()); int day = integer.parseint(stk.nexttoken()); int year = integer.parseint(stk.nexttoken()); double weight = double.parsedouble(stk.nexttoken()); dog list = new dog(name, breed, month, day, year, weight); arraylist<dog> dogs = new arraylist<dog>(); dogs.add(list); double firstweight = dogs.get(0).getweight(); system.out.println(firstweight); (int = 0; i< dogs.size(); i++) { double newweight = dogs.get(i).getweight(); } public class dog { private string dogname; private string dogbreed; private int dogmonth; private int dogday; private int dogyear; private double dogweight; public dog(string name, string breed, int month, int day, int year, double weight) { dogname = name; dogbreed = breed; dogmonth = month; dogday = day; dogyear = year; dogweight = weight; } public string getname() { return dogname; } public string getbreed() { return dogbreed; } public string tostring() { return string.format("%d/%d/%d", dogmonth, dogday, dogyear); } public double getweight() { return dogweight; }
there couple things did right btu placed in wrong place. a) extracting values line, b) creating dog instance, c) adding dog list, should happen inside loop. otherwise last value in file.
arraylist<dog> dogs = new arraylist<dog>(); while(sc.hasnextline()) { // read line input file via sc line line = sc.nextline(); stringtokenizer stk = new stringtokenizer(line); string name = stk.nexttoken(); string breed = stk.nexttoken(); int month = integer.parseint(stk.nexttoken()); int day = integer.parseint(stk.nexttoken()); int year = integer.parseint(stk.nexttoken()); double weight = double.parsedouble(stk.nexttoken()); dog list = new dog(name, breed, month, day, year, weight); dogs.add(list); }
update
int lightestindex = 0; int heaviestindex = 0; (int = 0; i< dogs.size(); i++) { if(dogs.get(i).getweight() <= dogs.get(lightestindex).getweight()){ lightestindex = i; } if(dogs.get(i).getweight() >= dogs.get(heaviestindex).getweight()){ heaviestindex = i; } } system.out.println("the lightest dog " + dogs.get(lightestindex).tostring()); system.out.println("the heaviest dog " + dogs.get(heaviestindex).tostring());
Comments
Post a Comment