6 Replies - 13794 Views - Last Post: 16 April 2009 - 04:18 PM Rate Topic: -----

#1 dsptl  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 09-February 09

cannot find symbol symbol : method length()

Posted 15 April 2009 - 08:10 PM

import java.util.*;
import java.io.*;
public class word{
public static void main(String[] args)throws FileNotFoundException
{
String filename;
String words;
String line;
String characters;
int totalCharacters;
int count = 0;
int countword = 0;
int countCharacters = 0; 

try{
Scanner in = new Scanner(System.in);
System.out.print("Enter name of input file: ");
Scanner input=new Scanner(new FileReader(in.nextLine()));


if(!input.hasNext())
	{System.out.println("File Empty - aborting");
	System.exit(0);}
while (input.hasNextLine())
		{
			line = input.nextLine();					
			count++; 
			Scanner inLine = new Scanner(line);	
			while (inLine.hasNext())
			{
				words = inLine.next();
				countword++;
			}
			   Scanner allWords = new Scanner(line);
			   while (allWords.hasNext())
			   {
				   characters = allWords.next();
				   countCharacters += characters.length();
			   }
		} 
int vowelCount = 0;

	for (int i = 0; i < in.length(); i++) {
	  char c = in.charAt(i);
	  if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i')
		  || (c == 'O') || (c == 'o') || (c == 'U') || (c == 'u'))
		vowelCount++;
	}
		System.out.println("Number of lines: " + count);
		System.out.println("Number of words: " + countword);
		System.out.println("Number of characters: " + countCharacters);
		


PrintStream out=new PrintStream(new File("output.txt"));
out.println("Number of lines: " + count);
out.println("Number of words: " + countword);
out.println("Number of characters: " + countCharacters);
out.close();
System.exit(0);}
catch ( FileNotFoundException e)
{System.out.println("C:\\ The file you entered either do not exist or the name is spelled wrong.");}
	
}
}



Error:

C:\Users\Dilip\CSC2310\word.java:44: cannot find symbol
symbol  : method length()
location: class java.util.Scanner
	for (int i = 0; i < in.length(); i++) {
						  ^
C:\Users\Dilip\CSC2310\word.java:45: cannot find symbol
symbol  : method charAt(int)
location: class java.util.Scanner
	  char c = in.charAt(i);
				 ^
2 errors


Is This A Good Question/Topic? 0
  • +

Replies To: cannot find symbol symbol : method length()

#2 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5062
  • View blog
  • Posts: 26,437
  • Joined: 10-May 07

Re: cannot find symbol symbol : method length()

Posted 15 April 2009 - 08:15 PM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: cannot find symbol symbol : method length()

Posted 15 April 2009 - 08:22 PM

Scanner class does not have a length() method
You wanted it to return the length of what ?

neither dows it have a charAt() method :D
Was This Post Helpful? 0
  • +
  • -

#4 dsptl  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 09-February 09

Re: cannot find symbol symbol : method length()

Posted 15 April 2009 - 08:37 PM

View Postpbl, on 15 Apr, 2009 - 07:22 PM, said:

Scanner class does not have a length() method
You wanted it to return the length of what ?

neither dows it have a charAt() method :D


actually I want to count the number of total vowel from the input file
Was This Post Helpful? 0
  • +
  • -

#5 dsptl  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 09-February 09

Re: cannot find symbol symbol : method length()

Posted 15 April 2009 - 09:30 PM

import java.util.*;
import java.io.*;
import jpb.*;
public class word{
public static void main(String[] args)throws FileNotFoundException
{
String filename;
String words;
String line;
String characters;
int totalCharacters;
String s; 
int count = 0;
int countword = 0;
int countCharacters = 0;
int vowelCount = 0;

try{
Scanner in = new Scanner(System.in);
System.out.print("Enter name of input file: ");
Scanner input=new Scanner(new FileReader(in.nextLine()));


if(!input.hasNext())
	{System.out.println("File Empty - aborting");
	System.exit(0);}
while (input.hasNextLine())
		{
			line = input.nextLine();					
			count++; 
			Scanner inLine = new Scanner(line);	
			while (inLine.hasNext())
			{
				words = inLine.next();
				countword++;
			}
			   Scanner allWords = new Scanner(line);
			   while (allWords.hasNext())
			   {
				   characters = allWords.next();
				   countCharacters += characters.length();
			   }
while (input.hasNextLine())
		{		 
		  s = input.nextLine();
		  s = s.toLowerCase();		
		  for (int i = 0; i < s.length(); i++) {
		  char c = s.charAt(i);
		  if ((c == 'a') || (c == 'e') || (c == 'i')|| (c == 'o') || (c == 'u'))
		  vowelCount++;
		}
	}	
}
		System.out.println("Number of words: " + countword);
		System.out.println("Number of lines: " + count);
		System.out.println("Number of sentences: ");
		System.out.println("Number of vowels: " + vowelCount);
		System.out.println("Number of characters: " + countCharacters);
		System.out.println("Number of punctuations: ");


PrintStream out=new PrintStream(new File("output.txt"));
out.println("Number of words: " + countword);
out.println("Number of lines: " + count);
out.println("Number of sentences: ");
out.println("Number of vowels: " + vowelCount);
out.println("Number of characters: " + countCharacters);
out.println("Number of punctuations: ");
out.close();
System.exit(0);}
catch ( FileNotFoundException e)
{System.out.println("C:\\ The file you entered either do not exist or the name is spelled wrong.");}
	
}
}


OutPut:

Enter name of input file: read.txt
Number of words: 6
Number of lines: 1
Number of sentences: 
Number of vowels: 0
Number of characters: 28
Number of punctuations: 

Process completed.



Why this shows me 0 vowels?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: cannot find symbol symbol : method length()

Posted 16 April 2009 - 04:03 PM

I would be more puzzled by the number of lines: 1
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: cannot find symbol symbol : method length()

Posted 16 April 2009 - 04:18 PM

A few System.out.print would have showed where your error is:

I removed the useless Scanner to count the characters... you already have the String line that contains them

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

public class word{
	public static void main(String[] args)throws FileNotFoundException
	{
		String filename;
		String words;
		String line;
		String characters;
		int totalCharacters;
		String s; 
		int count = 0;
		int countword = 0;
		int countCharacters = 0;
		int vowelCount = 0;

		try{
			Scanner in = new Scanner(System.in);
			System.out.print("Enter name of input file: ");
			Scanner input=new Scanner(new FileReader(in.nextLine()));


			if(!input.hasNext())
			{System.out.println("File Empty - aborting");
			System.exit(0);}
			while (input.hasNextLine())
			{
				line = input.nextLine();  
				System.out.println("Line: " + line);
				count++; 
				Scanner inLine = new Scanner(line);
				System.out.print("Words:");
				while (inLine.hasNext())
				{
					words = inLine.next();
					System.out.print(" " + words);
					countword++;
				}
				countCharacters += line.length();
				for(int i = 0; i < line.length(); i++) {
					char c = line.charAt(i);
					if ((c == 'a') || (c == 'e') || (c == 'i')|| (c == 'o') || (c == 'u'))
						vowelCount++;
				}	
			}
			System.out.println("Number of words: " + countword);
			System.out.println("Number of lines: " + count);
			System.out.println("Number of sentences: ");
			System.out.println("Number of vowels: " + vowelCount);
			System.out.println("Number of characters: " + countCharacters);
			System.out.println("Number of punctuations: ");


			PrintStream out=new PrintStream(new File("output.txt"));
			out.println("Number of words: " + countword);
			out.println("Number of lines: " + count);
			out.println("Number of sentences: ");
			out.println("Number of vowels: " + vowelCount);
			out.println("Number of characters: " + countCharacters);
			out.println("Number of punctuations: ");
			out.close();
			System.exit(0);}
		catch ( FileNotFoundException e)
		{System.out.println("C:\\ The file you entered either do not exist or the name is spelled wrong.");}

	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1