0

I'm new to Java, and struggling with something I've never had trouble with in the past. For whatever reason, I can't scan an int (or a double) in my code, but I can scan a string just fine. I'm posting the snippet where my scanner isn't functioning, please let me know if I should include the rest of the program.

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;

public class DZP3
{
    public static void main(String[] args) throws IOException
    {
        announce();
        Scanner scan = new Scanner(System.in);
        //Prompt user for input file
        System.out.println("Greetings! Please enter the filename of the plaintext olympics     data file you'd like to open.");
        String txtFilename = scan.nextLine();

        //Opens olympics data txt file specified, exits if it does not exist
        File medalsInput = new File (txtFilename);
        if(!medalsInput.exists())
        {
            System.out.println("File not found. Reload and try again.");
            System.exit(1);
        }
        //Prompt user for output file
        System.out.println("Thanks. Please enter the filename of the plaintext data output file.");
        String outputTxt = scan.nextLine();
        //Create output file specified
        File medalsOutput = new File (outputTxt);

        //Prompt user for medal cutoff X value
        System.out.println("Thanks. Please enter the minimum number of medals a nation must have earned to be counted for calculation 2 listed above. \nEnter the value, as an integer:");
        int medalsCutoff = 0;
        medalsCutoff = scan.nextInt();
        fileProcessing(medalsInput, medalsOutput, medalsCutoff);



    }

}

Near the bottom, medalsCutoff is not accepting any scanned value whatsoever. I've tried putting it in a method other than main, I've tried rearranging it, creating a separate scanner just for it, and a few other things. The debugger shows that, no matter what, I'm stuck on that line of code. What have I done wrong? I'm at a loss.

EDIT: Here's the fileProcessing method, and what comes after. The announce method is just system.out.println.

 public static void fileProcessing(File medalsIn, File medalsOut, int medalsMin) throws IOException
{
  //Initialize necessary variables and strings
  int maxTotMedals = -1;
  int natCountMedalsMin = 0;
  int natHiScore = -1;
  String natName;
  String answerOne = "DEFAULT";
  int answerTwo = 0;
  String answerFour = "DEFAULT";


  //Create Printwriter
  PrintWriter pw = new PrintWriter(medalsOut);
  //Create scanner to read from file, loop until end of file
  Scanner filescan = new Scanner(medalsIn);
  while (filescan.hasNext())
     {
        //Initializes medal counting variables at zero, resetting the values with each line
        int gCount = 0;
        int sCount = 0;
        int bCount = 0;
        natName = filescan.next();
        int lineMedals = 0;
        while (lineMedals < 4); //Runs 4 times to cover all four years
         {
           gCount += filescan.nextInt();
           sCount += filescan.nextInt();
           bCount += filescan.nextInt();
           lineMedals++;
         }
        int totalMedals = gCount + sCount + bCount;
        //Sees if this line's medals have exceeded previous total medal record, if yes, sets country name as answer to question one
        if (totalMedals > maxTotMedals)
           {
              answerOne = natName;
              maxTotMedals = totalMedals;
           }
        if (totalMedals >= medalsMin)
           {
              natCountMedalsMin++; //For answer two
           }    
        //Score calculation
        int natScore = gCount*3;
        natScore += sCount*2;
        natScore += bCount; 
        //Compares score to highest score, for answer four
        if (natScore > natHiScore)
           {
              answerFour = natName;
              natHiScore = natScore;
           }
        //Write nation name and score to file
        pw.println(natName + " " + natScore);           
     }

  //Define answer two after all countries have been counted
  answerTwo = natCountMedalsMin;
  //Close output file
  pw.close();
  //Send results to answer method
  answerPrint(answerOne, answerTwo, answerFour, medalsMin, natHiScore);
}

//This method outputs the answers to the user.
public static void answerPrint(String answerEin, int answerZwei, String answerVier, int medalsMini, int HiScore)
  {
  System.out.println("File read successfully.");
  System.out.println("The nation that earned the greatest number of medals is " + answerEin + ".");
  System.out.println(answerZwei + " countries earned more than " + medalsMini + " medals.");
  System.out.println("The nation with the highest score is " + answerVier + " with a score of " + HiScore + ".");
  System.out.println("Thank you for using this program. Until next time!");

}

EDIT 2: This has been solved, I had a stray semicolon in my fileProcessing method that caused an infinite loop. Thank you all for your help.

  • Did you check that this line System.exit(1); not executed ?? – Kick Mar 14 '14 at 06:28
  • By being stuck on that line of code do you mean that Program is not terminating? – AbhinavRanjan Mar 14 '14 at 06:28
  • Sorry I wasn't more specific. System.exit(1) only executes when the file is not found. It seems to be working properly. And the program is not terminating, it hangs like a lemon on the line `medalsCutoff = scan.nextInt();` until I terminate it manually. – user3418631 Mar 14 '14 at 06:30
  • well, I've tried it and it's running fine in my computer.. perhaps other part is having an issue.. – Yohanes Khosiawan 许先汉 Mar 14 '14 at 06:32
  • Can you give more details about fileProcessing? I think problem may be there. – AbhinavRanjan Mar 14 '14 at 06:33
  • @user3418631 Please give us an example of the input you are entering when this problem occurs. – Jason C Mar 14 '14 at 06:34
  • medalsCutoff=Interger.parseInt(scan.next()); – Benjamin Mar 14 '14 at 06:34
  • From what jgrasp's debugger is telling me, I'm not even getting to the fileprocessing method. That method reads in the user specified file, and in a rather convoluted bit of wizardry it answers questions based on the data read in, using a new scanner object that is only used to read the plaintext input file. – user3418631 Mar 14 '14 at 06:35
  • Its working fine. Just a simple doubt, did u give any input when it is waiting for an integer at that line? – Manu Viswam Mar 14 '14 at 06:36
  • Have a look at this http://stackoverflow.com/questions/12832006/java-scanner-error-with-nextint – AbhinavRanjan Mar 14 '14 at 06:36
  • I've tried inputting a very large number of letters, and I've tried strings. Even with a hasNextInt if statement, it just ignores everything I enter. I'll go ahead and post the rest of my code, to see if the problem is elsewhere. – user3418631 Mar 14 '14 at 06:37
  • If I comment out announce() and fileProcessing(), the code works fine for me..Can you paste your console output once? – AbhinavRanjan Mar 14 '14 at 06:41
  • I just tried this bit without the methods and it worked fine, so you folks are right, there's something up with announce and fileProcessing. I'll edit those into my post. – user3418631 Mar 14 '14 at 06:42
  • Also try to post a sample output from console. – AbhinavRanjan Mar 14 '14 at 06:44
  • Console output: ----jGRASP: connected to debugger. (announce goes here, over char limit) Greetings! Please enter the filename of the plaintext olympics data file you'd like to open. olympicsmedals.txt Thanks. Please enter the filename of the plaintext data output file. bingo.txt Thanks. Please enter the minimum number of medals a nation must have earned to be counted for calculation 2 listed above. Enter the value, as an integer: 11 5 pineapple 0 5 1 why – user3418631 Mar 14 '14 at 06:46
  • well, `fileProcessing(..)` comes after the `medalCutoff` input scanning, so how can that disturb it..? – Yohanes Khosiawan 许先汉 Mar 14 '14 at 06:49
  • That was just what I was wondering, @YohanesKhosiawan许先汉. I just learned methods this week, though, so I may have broken something somehow. – user3418631 Mar 14 '14 at 06:50
  • It was just a stray semicolon. A one of a kind mistake, to be sure. I'll just go get to work on my dunce cap and find a comfy corner to sit in... – user3418631 Mar 14 '14 at 06:57
  • yea, I just saw it too.. that `while (lineMedals < 4);` – Yohanes Khosiawan 许先汉 Mar 14 '14 at 06:57

2 Answers2

1
while (lineMedals < 4);

Above line has a semicolon at the end. It is an infinite loop.

AbhinavRanjan
  • 1,638
  • 17
  • 21
  • 1
    And the gold medal goes to... ^ – Jason C Mar 14 '14 at 06:54
  • You got it. That's quite the embarrassing mistake but I doubt I would have caught it without your help. Thank you @AbhinavRanjan. I'm surprised I didn't have more mistakes littered in there. – user3418631 Mar 14 '14 at 06:56
  • @user3418631 I think you could have found it yourself. Some people can see this just by looking at the code. I used debugger to go through every line and identify the line which is causing problem. – AbhinavRanjan Mar 14 '14 at 07:00
0

after file creation ,you use this below method File medalsOutput = new File (outputTxt); medalsOutput.createNewFile()

in ur code file not got created and exiting via syste.exit(1)