Jump to content

[urgent] help! [beginner]

- - - - -

  • Please log in to reply
9 replies to this topic

#1
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hey, guys I have an assignment due tomorrow and I am really lost, if anyone can give me some example code or explain to me how to this I would be greatful, I could do it probably if I could set the words, but the user input is where I get lost, in the sense of java counting the letters and matching it with the indexes, this is what it is:


Quote

Write a program that lets you enter any word and prints it out in various ways. The design and analysis of this problem should include techniques learned in class such as the flowchart and pseudocode. The algorithm must include repetition structures and use String methods deemed necessary such as charAt(), indexOf(), length(), substring(). Your input
Sample output:

It has to display like:



Canada
Canad
Cana
Can
Ca
C


C-a-n-a-d-a
C-a-n-a-d
C-a-n-a
C-a-n
C-a
C


Canada
anada
nada
ada
da
a

I am using Ready to Program IDE but any code would be helpful, please guys :(?

#2
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
Well I got the third one, I'm using for loops, I hope you've covered those in your class. I'll keep working on the first one, the second one should be as simple as concatenating a dash in between every letter.


For the third one you need a for loop to run for the length of the loop. For a String input you would use input.length().The loop will run through all the characters in the string. Printing it out is as easy as calling a substring of input to where you are in the loop.


for(int i = 0; i < input.length(); i++)

{

	System.out.println(input.substring(i));

}


#3
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Thank you so much nicckk, I really appreciate this help.

#4
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
I don't understand the manipulation part, when you're done yours could you please post it?

    c.println("Please enter your word");
    String word = c.readString();
    int len = word.length();
    c.println(len);
      int i;
      for (i = 1; i <=len; i++)
         c.println(word);


#5
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
The first one:

for(int i = a.length() - 1; i >= 0; i--) {
       System.out.println(a.substring(0,i));
}


#6
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Thank you! Anyone who helps who wouldn't mind an extra buck in their paypal pm me your pp email :)

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
2.
String input = "Canada";

        for( int i=input.length() ; i>0 ; i--){

            for(int j=0 ; j<i ; j++){

                System.out.print(input.charAt(j));

                if(j!=i-1){

                    System.out.print("-");

                }

            }

            System.out.println("");

        }


#8
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Thank you guys could anyone explain to me how the code works :$ ?

#9
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Here is the above code explained with comments.

// is a one line comment

/*
* multiple line comment
*/

This is fully functional; type code(if you use the code dont just copy and paste; you will learn alot more), compile and run.


// import scanner - used for input

import java.util.Scanner;


// a class

public class NewClass {


    // This is the main method needed to run program

    public static void main(String[] arrrgss) {


        // instanciate Scanner for user input

        Scanner input = new Scanner(System.in);


        // ask the user to enter name

        System.out.print("Enter a name:  ");


        //store the input into name using scanner

        String name = input.next();


        /*

         * Xdawn90s method

         * 1.He takes the length of name to be the starting index,

         *   then says as long as the index is greater than 0 keep running,

         *   for every time loop decrement the index(i)

         */

        for (int i = name.length(); i >= 0; i--) {

            // print name from 0 to index

            System.out.println(name.substring(0, i));

        }// end of for loop

        

        /*

         * wim DC method

         * The first for loop does the same as xdawns loop

         * but with a twist...

         */

        for (int i = name.length(); i > 0; i--) {

            /*

             * ...wim DC uses a second for loop to add the characters

             * one letter at a time by using j to index the char in string,

             * as long as j less than index i(prevents exception), 

             * but increment the next char before exiting the loop 

             */

            for (int j = 0; j < i; j++) {


                // print the letter at "charAt(0)"-"charAt(1)"-...

                System.out.print(name.charAt(j));


                // this prevents the - from being put at last letter

                if (j != i - 1) {

                    System.out.print("-");

                }// end of if


            }// end of inner for loop


            // this is just to go to next line

            System.out.println("");


        }// end of outer for loop


        /*

         * nicckks' method

         * This is similar to Xdawns method, only in reverse order

         */

        for (int i = 0; i < name.length(); i++) {

            System.out.println(name.substring(i));

        }// end of for loop

    }// end of main method

}// end of NewClass


Hope this explains the previously written code and aides you in learning.

Edited by mr mike, 05 January 2011 - 06:41 PM.
Added whitespace in code


#10
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Thanks alot everyone! Please guys pm me if you would like some paypal $ if you helped me :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users