Help please! Uni project - beginner!
Hi guys,
Im not sure where to start with this program but here is the specification. If you can help me out it would be appreciated! please include comments! Many thanks in advance if any of you genius's can help me.
Here is the spec:
Develop a Java program that asks the user to enter a phrase, and then outputs:
o the first character of the phrase
o the first and last indices of the character 'a' in the phrase
o the substring that begins at position 2 of the phrase
o a new phrase created by replacing all the occurrences of the character 'a' in the original phrase with the character '*'
o the entered phrase with all the letters in UPPER CASE
Example output:
You entered the phrase "Java is great!"
The first character of the phrase is 'J'
The character 'a' occurs at positions 1 and 11
The substring beginning at position 2 is "va is great!"
Replacing 'a' with '*' gives "J*v* is gre*t!"
In upper case the phrase is "JAVA IS GREAT!"
[1102 byte] By [
xjairusx] at [2007-11-20 11:29:27]

# 2 Re: Help please! Uni project - beginner!
What I have so far, no idea how to count the a's and and also not sure why its saying cannot find symbol for the kybd.
// A program to select characters from there positions and replace the characters out of a string
import java.util.*; // the package containing Scanner
public class StringWork
{
public static void main(String[] args)
{
// Build an instance of Scanner, that is an object
Scanner kybd = new Scanner(System.in);
// Input a phrase
String phrase = kybd.nextString();
int phraseLength = phrase.length();
char firstLetter = phrase.charAt(0);
char lastLetter = phrase.charAt(phraseLength - 1);
String subString = phrase.subString(2);
String newString = phrase.replace('a', '*');
//Print the results
System.out.println("You entered the phrase: " + phrase );
System.out.println("The first character of the phrase is " + firstLetter );
System.out.println("The character \'a\' occurs at positions " + firstTime + secondTime );
System.out.println("The substring that begins at position 2 is: " + subString );
System.out.println("Replacing \'a\' with \'*\' gives: " + newString );
System.out.println("In upper case the phrase is " + phrase.toUpperCase());
}
}
Errors:
javac StringWork.java
StringWork.java:14: cannot find symbol
symbol : method nextString()
location: class java.util.Scanner
String phrase = kybd.nextString();
^
StringWork.java:18: cannot find symbol
symbol : method subString(int)
location: class java.lang.String
String subString = phrase.subString(2);
^
StringWork.java:24: cannot find symbol
symbol : variable firstTime
location: class StringWork
System.out.println("The character \'a\' occurs at positions " + firstTim
e + secondTime );
^
StringWork.java:24: cannot find symbol
symbol : variable secondTime
location: class StringWork
System.out.println("The character \'a\' occurs at positions " + firstTim
e + secondTime );
^
4 errors
I have no idea how to count an a, please :'( lol. and any idea why its saying cannot find symbol there?
# 3 Re: Help please! Uni project - beginner!
StringWork.java:14: cannot find symbol
symbol : method nextString()
There is no "nextString()" method in the Scanner class. The method "next()" reads the first token as a string. If you want the whole phrase in one string, nextLine() is what you want...
.
StringWork.java:18: cannot find symbol
symbol : method subString(int)
There is no "subString(int)" method in the String class. There is, however, a "substring(int)" class.
.
StringWork.java:24: cannot find symbol
symbol : variable firstTime
StringWork.java:24: cannot find symbol
symbol : variable secondTime
These are here because you haven't defined them. To find the indices of the letter "a", look up the "index" methods of the String class.
masher at 2007-11-10 2:16:09 >
