|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Student needs help.
I need help like yesterday. I am in a programing class at vatterott College & We are supposed to create a program on Java that counts how many time each letter in the alphebet appear in the word Supercalifragilisticexpialidocious. How do I get the program to count through a loop in this manner. I am very new to programing and really need help.
thank you. |
|
#2
|
|||
|
|||
|
// This is done entirely with raw force..
// So more elegant approaches will exist // But it works.. // See the Java API at java.sun.com // if you don't know some of the method // calls. // The only "Special" classes I use, are // String and java.util.Vector // If you want to learn something // read it through and then write it //yourself - and make it better // (The double loops (that I use) // often tend to go to these O(n^2) // Algorithms, but actually in this // case the complexity // is asymptotically O(n) - can you see // why? But of course a much better // O(n) alg (with smaller coefficient) // can be // found that does the same thing. /* * Created on Sep 10, 2004 * */ package test; import java.util.Vector; /** * @author * */ public class countLetters { /** * */ public countLetters(String[] args) { if (args == null || args.length < 0) System.out.println("No letters"); Vector letters = new Vector(); String s; Vector words = new Vector(); for (int i=0;i<args.length;i++){ s = args[i]; if (s != null){ putLettersFromStringToVector(s, letters); words.add(s); } } System.out.println("The words \n\""+words+"\"\n have the following letters:\n"); Letter l; for (int i=0;i<letters.size();i++){ l = (Letter)letters.get(i); if (l != null){ System.out.println("The letter \""+l.letter+"\" is repeated "+l.numberOfTimes+" times"); } } } public static void main(String[] args) { // new countLetters(args); String[] ss = {"Vittu" ,"Perkele", "SaAaaTana"}; new countLetters(ss); } public void putLettersFromStringToVector(String s,Vector v){ if (s == null) return; String s2 = s.toLowerCase(); String s3; Letter l; boolean found; for (int i=0;i<s.length();i++){ s3 = s2.substring(i, i+1); found = false; for (int j=0;j<v.size() && !found;j++){ l = (Letter)v.get(j); if (l != null){ if (l.letter.compareTo(s3) == 0){ l.numberOfTimes++; found = true; } } } if (!found){ v.add(new Letter(s3,1)); } } } private class Letter { private String letter; private int numberOfTimes = 0; private Letter(String letter,int numberOfTimes){ this.letter = letter; this.numberOfTimes = numberOfTimes; } } } // What? The editor doesn't take // tabs?? The code looks horrible // without indentations. |
|
#3
|
|||
|
|||
|
think twice about using posted code in your project
Quote:
I have heard of quite students in comp sci classes that ask for code on forums that their teachers read, they get caught using posted code, and they get either expelled or severely reprimanded(including failure of the class). If you want to use forums, I believe it's ok to ask for specific help such as "How do I instantiate an object in Java?" or "How to I print output to the console?" etc. I suggest checking with your professor first and get their policy on using forums for help with projects. You don't want all your hard work, time , and money you spent going to college to go for nothing if you get kicked out for some silly little java project. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Student needs help. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|