Question

I'm trying to finish my lab in my intro to CS course and am having trouble reading in information from txt files and using it.

Below is my code so far:

import java.io.*;
import java.util.*;

public class loops {
public static void main (String [] args) throws FileNotFoundException{

File myFile = new File("Snowboard_Scores.txt");
Scanner input = new Scanner(myFile);

String name;
String country;
int snow1;
int snow2;
int snow3;
double score;
int max = 0;

while (input.hasNext() ){
    System.out.println(input.nextLine());
    max++;
}
System.out.println(max);

for (int count = 0; count < max; count ++){

}
}
}

The lab is simple, I'm comparing three scores from 4+ different snowboarders. Each snowboarder has a name, a country and 3 scores from judges. I need to average the scores, then compare it to all the snowboarders to see who had the best score and then print that snowboarders name and country.

I'm struggling with collecting and storing all the data in a way that will be useful. WE CANNOT USE ARRAYS at this point in our labs, anybody have any ideas on how to go about this?

Snowboard_Scores.txt:

 Shaun White
 United States
 9.7
 9.8
 9.6
 Bob Saget
 Yugolsavia
 1.4
 2.1
 1.9
 Morgan Freeman
 Antartica
 10.0 
 9.9
 9.8
 Diana Natalicio    
 Brazil
 8.7
 8.7
 9.2 
Was it helpful?

Solution

its too simple;
process data while you read data;
read each snowboard and compare with best prev value;
then best is selected as best value; see code below

import java.io.*;
import java.util.*;

public class loops {
public static void main (String [] args) throws FileNotFoundException{

    File myFile = new File("Snowboard_Scores.txt");
    Scanner input = new Scanner(myFile);

    String name = "";
    String country = "";
    double score = 0.0;

    while (input.hasNext() ){
        String tmp_name = input.nextLine();
        String tmp_country = input.nextLine();
        double tmp = Double.parseDouble(input.nextLine())/3;
        tmp += Double.parseDouble(input.nextLine())/3;
        tmp += Double.parseDouble(input.nextLine())/3;
        if(tmp > score){
            name = tmp_name;
            country = tmp_county;
            score = tmp;
        }
    }
    System.out.println(name);
    System.out.println(country);
}

you right. one line missed

OTHER TIPS

Just a slight modification.

while (input.hasNext()){

 String tmp_name = input.nextLine();
 String tmp_country = input.nextLine();

 double tmp_avg = Double.parseDouble(input.nextLine())/3;
 tmp_avg += Double.parseDouble(input.nextLine())/3;
 tmp_avg += Double.parseDouble(input.nextLine())/3;

 if(tmp_avg > score){
  score = tmp_avg;
  name = tmp_name;
  country = tmp_country;
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top