Question:
Need help with practice java programming question?
Lefty
2010-12-09 18:21:23 UTC
how do I make a class that reads an text file like this
2
3
WHOPPER
CHICKEN SANDWICH
CHICKEN SANDWICH
2
WHOPPER
WHOPPER
2
CHICKEN SANDWICH
WHOPPER

(where the first integer is the number of data sets, the second is the number of food items in the first data set...etc)
and prints the total fat conent for each data set on a separate line?

The output should be...
90g
128g
77g

the fat content of a whopper is 64g, and the sandwich is 13g. Those are my only two values for food.
I am inexperienced in java(obviously) but have been working at this for several hours. This is what I have so far...(please dont laugh, ive never dealt with exceptions or try statements before)
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.IOException;

public class FatCounter
{
public static void main(String[] args)
throws FileNotFoundException, IOException
{
Scanner console = new Scanner(System.in);
FileReader reader = new FileReader("input.txt");
Scanner in = new Scanner(reader);

try
{
int sets = in.nextInt();
int foods = in.nextInt();

while(in.hasNextLine())
{
for (int i = 1; i<=foods;i++)
{
//confused on the loop structure here
}
}
}
finally //not sure what to put here
{
System.exit(0);//?
}
}
}

(sorry about the lack of indentation, its yahoo, not me)
Three answers:
2010-12-09 22:40:35 UTC
The first line in input.txt should be 3. change the try block as below



try

{

int sets = in.nextInt();



for(int i=0;i
int foods = in.nextInt();



String s="";

in.nextLine();

int total=0;

for(int j=0;j
s=in.nextLine();



if(s.equals("CHICKEN SANDWICH")){

total=total+13;

}

if(s.equals("WHOPPER")){

total=total+64;

}



}

System.out.println(total+"g");

}



}
bryington
2016-10-19 03:45:25 UTC
Linear seek and bubble form are no longer recursive algorithms, doing them recursively is a stable thank you to get a stack overflow blunders. That aside...i think of the main suitable suggestion i'm going to supply is 'use your mind's eye'. i assume it relies upon what form of guy or woman you're, yet in my journey, reinventing somebody else's wheel is seldom the main relaxing and academic ingredient to do. case in point, attempt writing some style of trouble-free card game or maze game, if conceived interior the the main suitable option way those would desire to lend themselves to OO programming and you will learn swifter in case you're able to return up which comprise your own application good judgment.
2010-12-09 19:06:17 UTC
I understand reading and writing files in Java and would like to help but i don't understand this



2

3

WHOPPER

CHICKEN SANDWICH

CHICKEN SANDWICH

2

WHOPPER

WHOPPER

2

CHICKEN SANDWICH

WHOPPER



if that is in the text file where do you get this



90g

128g

77g


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...