1 Replies - 739 Views - Last Post: 21 July 2012 - 03:52 PM Rate Topic: -----

#1 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

can't seem to know how to fix my errors

Posted 21 July 2012 - 03:46 PM

I am trying to write a program that reads a text from notepad and the user enters a letter and the program tells the user how many times that letter occurred in the file.

I keep getting these errors. the toUpperCase() and the charAt() keep giving me errors? Why isnt my program working? Thank you!

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

public class Letter
{
	public static void main(String[] args)
	{
		String fileName;
		String letter;
		char caps;
		String str;
		int sum = 0;
		String big;
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.print("Enter in a filename. ");
		fileName = keyboard.nextLine();
		
		FileReader freader = new FileReader(fileName);
		BufferedReader inputFile = new BufferedReader(freader);
		
		System.out.println("Enter a letter. ");
		letter = keyboard.nextLine();

		caps = letter.toUpperCase();
		
		while (str != null)
		{
			for (int i = 0; str!= null; i++)
			{
				if (caps.charAt(i) == big.charAt(0))
				{	
					sum += 1;
				}
			}
			
			str = inputFile.readLine();
			big = str.toUpperCase();
		}
		
		System.out.println("The number of times the letter occured is " + sum);
	}
}

		
				
		
		



Is This A Good Question/Topic? 0
  • +

Replies To: can't seem to know how to fix my errors

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: can't seem to know how to fix my errors

Posted 21 July 2012 - 03:52 PM

Line 26: 'caps' is char primitive and 'letter' is a String object. You should be getting an error like:

Type mismatch: cannot convert from String to char
Cannot invoke charAt(int) on the primitive type char
at Test.main(Test.java:26)

When you're getting errors that you want help with, post them, copied and pasted.

You have a similar problem (but different) on line 32 in that you're trying to invoke a method charAt() on the char 'caps.'
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1