English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

You dont have to give me the answer, just please point me in the right direction! Thanks.

Here is the question: Write a program that asks the user to enter a string, and then asks the user to enter a character. the program should count and display the number of times that the specified character appears in the string.

Here is what i have so far:
import java.util.Scanner;

public class letterCounter
{
public static void main(String[] args)
{
String creation; //This is the users imported string
String character; //This is the character the user enters
char letter;

Scanner keyboard = new Scanner(System.in); //This reads users inputs

System.out.println("Please enter a sentance.");
creation = keyboard.nextLine();

System.out.println("Now, please enter a letter.");
character = keyboard.nextLine();
letter = input.charAt(0);
}
}

I get an error stating that "cannot find system variable input"

please guide me as to what to do next!!
Thx

2006-11-28 05:56:53 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

You don't have to import any other java package - just java.util.Scanner is fine. The problem with your code is that you have not declared the variable 'input' anywhere but you are trying to get the first char from it.

Replace the line:
letter = input.charAt(0);
by
letter = character.charAt(0);

and your code will work... however you will need more to check the number of occurrences of letter in the creation. One way to do this would be -

1. Get the length of your String (creation) using creation.length()
2. Using a loop traverse the entire string, one char at a time - using charAt(x) where x is the loop var.
3. In each iteration of the loop, compare the letter with charAt(x), and increment the count if a match is found.

Hope this helps.

Additional Comments
================
I got your message on my yahoo mail seeking further help on this question... but I was not able to reply to your message as yahoo tells me - "Oops! Your email did not go through because the recipient\'s email address has not been confirmed."

Anyways, email me at your_taurean@yahoo.com and I will be able to assist you further.

2006-11-28 06:12:01 · answer #1 · answered by SmartSpider 4 · 1 0

i ask your self how lengthy it will take you to comprehend that besides the actual undeniable actuality that acquiring somebody else to do your homework is a appropriate short-time period answer, it doesn't truly teach you something about Java. likely the objective of your direction is to teach you Java, so that you're not from now on precisely helping your self through dishonest, are you?...

2016-10-07 22:15:01 · answer #2 · answered by ? 4 · 0 0

The problem isn't that you are messing any libraries. The problem is that you never declared "input" as a variable.

2006-11-28 06:12:04 · answer #3 · answered by Jackson 2 · 0 0

add this at the beginning:

import java.io.*;
import java.util.*;
import javax.swing.*;

You're missing some of the libraries for what you need to do.

2006-11-28 06:03:20 · answer #4 · answered by Chris S 5 · 0 0

fedest.com, questions and answers