04-25-2004, 08:37 AM | #1 (permalink) |
Crazy
|
need help w/java prog
I've been workin on this for a while and I'm stuck... any help you can give me would be greatly appreciated! The program is using 4 methods: main method will allow user to input a sentence stored as a string, backward method will output sentence backward, words method will output the # of words in sentence and repeat method will count how many times the first letter of the sentence appears w/in the sentence. This is what I came up with but I'm stuck.
import javax.swing.*; public class StringsAndArrays {//start class String str,s1,first; int w,num; public static void main(String[] args) {//start main final int num; String str,s1; int w ; int num[]; s1=JOptionPane.showInputDialog("Input a sentence"); for (w=0;w<str.length();w++) { System.out.println("" +w+ "" +str.charAt(w)); } str = backward(); }//end main public static String backward () {//start backward str.reverse(); System.out.println("The sentence, after being revised is: " +str); return str; }//end backward public static String words () {//start words System.out.println("The number of words in the sentence is: " +w); }//end words public static String repeat () {//start repeat System.out.println(first+" : is repeated: " +num+ " times."); }//end repeat }//end class (for the method that need to count the number of words I'm pretty sure that you count the spaces and add 1... but i dont know how to type it in correctly.) |
04-25-2004, 11:07 AM | #2 (permalink) | ||
Sky Piercer
Location: Ireland
|
You seem a little confused about where to declare your variables. If you declare a variable inside your main method, then you can only get at those variables while you are in that method.
So when you have Quote:
When you declare a varibale of the same name outside the main method: Quote:
In short your main method will be using one set of variables (the ones defined inside it) and the other methods wil be using another set of variables (the ones defined within the class). Anyway, I have fixed up your code, and written the two other methods (words and repeat). Code:
import javax.swing.*; public class StringsAndArrays {//start class public static void main(String[] args) {//start main str=JOptionPane.showInputDialog("Input a sentence"); backward(str); words(str); repeat(str); }//end main //Prints out the string s backwards public static void backward (String s) {//start backward s = s.reverse(); System.out.println("The sentence, after being revised is: " +s); }//end backward //Prints out the number of spaces in the string + 1 public static void words(String s) {//start words int w=1; for(int i=0; i<s.length(); i++){ if(s.charAt(i)==' '){ w++; } } System.out.println("The number of words in the sentence is: " +w); }//end words public static void repeat (String s) {//start repeat char c = s.charAt(0); int num=0; for(int i=0; i<s.length(); i++){ if(s.charAt(i) == c){ num++; } } System.out.println(""+ c +": is repeated: " +num+ " times."); }//end repeat }//end class It isn't perfect, as it will crash if the user enters a string of length 0 (just presses enter). The words method is also far from foolproof, as all it does is count the number of space characters and add one to it. there are better ways of counting the number of words, though this is probably sufficient for someone just learning. Hope you can understand it, if not just ask.
__________________
Last edited by CSflim; 04-25-2004 at 11:29 AM.. |
||
04-25-2004, 11:49 AM | #3 (permalink) |
Crazy
|
thanks a lot CSflim, that was a big help!!!! I inserted what I needed from what you said... the only thing is i am gettin one error on the s=s.reverse(); Do i have to import something before the class to make this reverse work?
Last edited by scoobydugan; 04-25-2004 at 12:05 PM.. |
04-25-2004, 12:45 PM | #4 (permalink) | |
Sky Piercer
Location: Ireland
|
Quote:
Withoud this method, you have to manually print out the reversed string. This should do the trick: Code:
//Prints out the string s backwards public static void backward (String s) {//start backward System.out.println("String reversed is: "); for(int i=s.length()-1; i>=0; i--){ System.out.print(s.charAt(i)); } System.out.println(); }//end backward
__________________
|
|
Tags |
prog, w or java |
|
|