i need help with a fileio problem. Iron man can only carry those who weigh less
ID: 652873 • Letter: I
Question
i need help with a fileio problem.
Iron man can only carry those who weigh less than 200 pounds. The hulk will save the rest.
The file is called "rescuse.txt." It has a list of a first name, a space, and their weight (in pounds). The very first line in the file is the number of people on the list. Here is what the file "rescue.txt" reads.
4
Dave 249
Sally 123
Pete 249
Allison 163
Sample output:
There are 4 people.
I cannot Save Dave - he needs to wait for the hulk.
I can save Sally.
I cannot save Pete - he needs to wait for the Hulk.
I can save Allison.
It says to use next() (which read the name into a String) and nextInt() (to read the names and numbers)
This is what I have so far. Please help.
inport java.io.File;
import java.util.Scanner;
public class save {
public static void main(String[] args) throws Exception {
File myFile = new File("rescue.txt"); //declare a variable of type File
Scanner fileRead = new Scanner(myFile); //and open a file for reading
int sum =0;
int count =0;
int number;
count = fileIn.next.Int(); //Read in the number of elements in the file
System.out.println("There are " + count + "items in the file");
number = fileIn.nextInt(); //// "prime the pump" - read in the first value
Explanation / Answer
/**
* The java program that reads a text file called
* "rescue.txt" that cotains the data values.
* Read the name and weight and check if the wieght
* is greater than 200 then print that he need to wait
* for hulk otherwise I can save that name.
* */
//Save.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Save
{
public static void main(String[] args) throws Exception
{
//use try-catch block to catch the exception if not found
try
{
//create an object of File and
//add a Scanner object to file to read input of file
File myFile = new File("rescue.txt"); //declare a variable of type File
Scanner fileRead = new Scanner(myFile); //open a file for reading
//To read name
String name;
//To read wight
int weight;
//To store the count of people in the file
int count =0;
//index to track number of people in the file
int index=0;
//Read in the number of elements in the file
count = Integer.parseInt(fileRead.nextLine());
System.out.println("There are " + count + " people");
while(fileRead.hasNextLine()&& index<count)
{
//read name
name = fileRead.next();
//read weight in pounds
weight=fileRead.nextInt();
if(weight<200)
System.out.println("I can save "+name+".");
else
System.out.println("I cannot Save "+name+" - he needs to wait for the hulk.");
//increment the count
count++;
}
}
//catch the exception if file not found
catch (FileNotFoundException e)
{
System.out.println("File not found");
}
}
}
//sample input
//rescue.txt
4
Dave 249
Sally 123
Pete 249
Allison 163
//sample output:
There are 4 people
I cannot Save Dave - he needs to wait for the hulk.
I can save Sally.
I cannot Save Pete - he needs to wait for the hulk.
I can save Allison.
Note : input file "rescue.txt" must be in the current working directory otherwiser specify the path where the file is located in File object.
Hope this helps you...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.