Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Someone please help me write a simple program!!!

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Someone please help me write a simple program!!!

    Requirements: Solution must employ at least one method other than the main() method.

    PLEASE HELP ME ASAP!!!!!
    This is an assignment for my beginner class, so please dont put in anything too crazy. However, not to easy
    Please make use of if/else statements, while loops, for loops, arrays, and system.out.printlns. If you cant its ok, but please try to.
    THANK YOU~

    p.s. if you can write a comment to describe the process of each line, it would be very helpful.
    Last edited by ocean123; June 10th, 2009 at 08:08 PM.


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Someone please help me write a simple program!!!

    hi,

    welcome to the forum.
    you can try this.

    It will invert the word you write.
    you can find too many programs here that has your requirements.

    or better try to do such things.

    it will be fun.

    import java.io.*;
     
    public class WordInverter {
     
     
     
     private static BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
     
        public  static void main(String[] args)throws IOException
     
         { 
     
           String strInv = null,word;
     
            System.out.print("Enter a word : ");
            word=br.readLine();
            strInv = invert(word);
     
     
        System.out.println("The inverted word  : " + strInv);
     
     
          }
     
    //another method
        public static String invert (String s) {
             String temp = "";
             for (int i=s.length()-1; i>=0; i--)
                  temp += s.charAt (i);
             return temp;
        }
     
    }

    Cheers,
    Truffy

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Someone please help me write a simple program!!!

    Hello ocean123, welcome to the Java Programming Forums

    Have you attempted any of this yet?

    I have written this example for you. It contains everything you asked for - if/else statement, while loop, for loops, array, and system.out.printlns.

    You will need to comment this yourself. It will help you to workout and understand what is going on. Please let me know if you get stuck..

    import java.util.Scanner;
     
    public class Ocean123 {
     
        /**
         * JavaProgrammingForums.com
         */
     
        public static void myMethod(String name){
     
            int i = 0;
            char[] myArray = new char[name.length()];
     
            for(int a = 0; a < name.length(); a++){
                myArray[a] = name.charAt(i);
                System.out.println(name.charAt(i));
                i++;
            }
     
            for(int a = 0; a < myArray.length; a++){
                System.out.print(myArray[a] + "*");
            }
     
            System.out.println();
            System.out.println("Enter a word: (bye to quit) ");
        }
     
     
        public static void main(String[] args) {
     
            Ocean123 ocean = new Ocean123();
     
            Scanner sc = new Scanner(System.in);
     
            System.out.println("Enter a word: (bye to quit) ");
     
            while(sc.hasNext()){
     
            String input = sc.next();
     
            if(input.equals("bye")){
                System.out.println("Bye! Bye!");
                System.exit(0);
            }else{
                ocean.myMethod(input);
            }
            }
     
        }
    }

    Output:

    Enter a word: (bye to quit)
    java
    j
    a
    v
    a
    j*a*v*a*
    Enter a word: (bye to quit)
    programming
    p
    r
    o
    g
    r
    a
    m
    m
    i
    n
    g
    p*r*o*g*r*a*m*m*i*n*g*
    Enter a word: (bye to quit)
    bye
    Bye! Bye!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: Someone please help me write a simple program!!!

    Im sorry to nitpick but I think "equalsIgnoreCase" would be a better idea here:

    if(input.equals("bye")){
                System.out.println("Bye! Bye!");
                System.exit(0);
            }else{
                ocean.myMethod(input);
            }
            }
     
        }
    }

Similar Threads

  1. How to Write a simple XMPP (Jabber) client using the Smack API
    By JavaPF in forum Java Networking Tutorials
    Replies: 34
    Last Post: May 15th, 2013, 02:25 AM
  2. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 03:33 PM
  3. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM
  4. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 02:33 PM