![]() |
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.) |
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.*; 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. |
thanks a lot CSflim, that was a big help!!!! :D 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?
|
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 |
It works thanks so much!!!!
|
All times are GMT -8. The time now is 02:41 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project