0

I have to read in a file to my project and then read the file line by line to find the percent of upper case and lower case letters. My output should be 4 lines stating the percent of upper and lower for that specific line in the file. This is what I have to far but it doesn't quite work:

    Scanner inFile = new Scanner(new File("file.txt"));

    int upper = 0;
    int lower = 0;
    int total;
    double percentU = 0.0;
    double percentL = 0.0;




    while (inFile.hasNext()) {
        String line = inFile.nextLine();

        for (int x = 0; x < line.length(); x++)
        {
            if (Character.isUpperCase(line.charAt(x)))
            {
                upper++;
            }
            if (Character.isLowerCase(line.charAt(x)))
            {
                lower++;
            }

        total = upper + lower;
        percentU = upper/total;
        percentL = lower/total;    

        System.out.println("lowercase: " + String.format("%.2f", percentL) + "\t" + "uppercase: " + String.format("%.2f", percentU));
        }



    }

2 Answers2

0

When you divide an int by another int the result will be also an int. So for example if you divide 30 by 50 the result will be 0 instead of 0.6. More information:
How to make the division of 2 ints produce a float instead of another int?
Integer division: How do you produce a double?
Division of integers in Java

Community
  • 1
  • 1
Saeid
  • 4,147
  • 7
  • 27
  • 43
0

I Just change these lines removed if (Character.isLowerCase(line.charAt(x))):

         if (Character.isUpperCase(line.charAt(x))) {
                    upper++;
                } 
           if (Character.isLowerCase(line.charAt(x))) {
                    lower++;
                }

To:

            if (Character.isUpperCase(line.charAt(x))) {
                    upper++;
                } else {
                    lower++;
                }

And moved these lines out of while loop:

 total = upper + lower;
    percentU = (float)upper/total;
    percentL = (float)lower/total;    
    System.out.println("total: " + total);
    System.out.println("lowercase: " + String.format("%.2f", percentL) + "\t" + "uppercase: " + String.format("%.2f", percentU));

Try this solution it's ok:

 public static void main(String[] args) {
    Scanner inFile = null;
    int upper = 0;
    int lower = 0;
    int total = 0;
    double percentU = 0.0;
    double percentL = 0.0;
    try {
        inFile = new Scanner(new File("C:\\temp\\file.txt"));

        while (inFile.hasNext()) {
            String line = inFile.nextLine();

            for (int x = 0; x < line.length(); x++) {
                if (Character.isUpperCase(line.charAt(x))) {
                    upper++;
                } else {
                    lower++;
                }

            }
        }
        total = upper + lower;
        percentU = (float)upper/total;
        percentL = (float)lower/total;    
        System.out.println("total: " + total);
        System.out.println("lowercase: " + String.format("%.2f", percentL) + "\t" + "uppercase: " + String.format("%.2f", percentU));

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Abdelhak
  • 8,299
  • 4
  • 22
  • 36