java - find the lowest age and print out the corresponding name in array from a file? -



java - find the lowest age and print out the corresponding name in array from a file? -

i need help programme reads in names , ages file called stuff.txt. able print both arrays, need find lowest age , print out corresponding name.my code not working , have no thought why? no loop or switch necessary. "stuff. txt" contains next input far:

sam 21 ram 22 hero 23

here have worked on far:

import java.io.*; import java.util.*; public class nameage { public static void main(string[] args) throws filenotfoundexception { arraylist<string> names = new arraylist<string>(); arraylist<integer> ages = new arraylist<integer>(); scanner infile = new scanner(new file("stuff.txt")); int = 0; while (infile.hasnextline()) { string line = infile.nextline(); scanner linescanner = new scanner(line); while(linescanner.hasnext()){ names.add(linescanner.next()); ages.add(linescanner.nextint()); } system.out.println(names); system.out.println(ages); } int idx = -1; int d= 100000; for( = 0; < 3; i++) if(ages < d) { d = ages[i]; idx = i; } system.out.println("lowest age "+ ages[idx] + "corresponding name" + name[idx] ); } }

slight modification of code posted excelledproducts

import java.io.*; import java.util.*; public class nameage { public static void main(string[] args) throws filenotfoundexception { arraylist<string> names = new arraylist<string>(); arraylist<integer> ages = new arraylist<integer>(); scanner infile = new scanner(new file("stuff.txt")); int = 0; while (infile.hasnextline()) { string line = infile.nextline(); scanner linescanner = new scanner(line); while(linescanner.hasnext()){ names.add(linescanner.next()); ages.add(linescanner.nextint()); } system.out.println(names); system.out.println(ages); } int old_age = 0; int old_age_i = 0; int count = 0; for(int age : ages){ if (old_age == 0) old_age = age; else if (old_age > age){ old_age = age; old_age_i = count; count ++;} else continue; } string name = names.get(old_age_i); system.out.println(name); string theage = ages.get(old_age_i); system.out.println(theage); } }

java

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -