must take user input and reverse each sentence that they write

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 1457 Views - Last Post: 18 September 2011 - 02:52 PM Rate Topic: -----

#1 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

must take user input and reverse each sentence that they write

Posted 17 September 2011 - 07:39 AM

HI everyone..What I have to do it ask the user how many sentences they want to input. The give me a number and I have to reverse each sentence but it has to be like Line 1: (here would be the reverse sentence), etc all the way untill all their sentences have been reversed with the corresponding Line number. This is what I did so far. I was able to reverse one sentence but then I tried doing more than one and now it wont run. this is how it should look like 
Input 
Output
this is a test
 Line #1: test a is this
foobar
 
Line #2: foobar

warm up programs Line #3: programs up warm



package lab2;

/**
 *
 * @author kyriakhskiathitis
 */
import java.util.Scanner;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner keyboard= new Scanner(System.in);
        
        System.out.println ("How many sentences are you inputing?");
        int answer=keyboard.nextInt();


       System.out.println("Please enter your sentences");
        String first=keyboard.nextLine();
        
        first = new StringBuffer(first).reverse().toString();
		System.out.println("String before reverse: "+first);
        





    }

}









Is This A Good Question/Topic? 0
  • +

Replies To: must take user input and reverse each sentence that they write

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 07:44 AM

You need a loop that takes the number of sentences the user answers will be entered. Take them all at once and store them in an array or take them one at a time.
Was This Post Helpful? 0
  • +
  • -

#3 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 07:45 AM

View PostGregBrannon, on 17 September 2011 - 07:44 AM, said:

You need a loop that takes the number of sentences the user answers will be entered. Take them all at once and store them in an array or take them one at a time.


can you show me an example??? Umm should I do a for loop??? and how would I store them in an array????
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 07:50 AM

You can find examples of loops (or control statements) at this tutorial. You can learn about using arrays here. Do your best at writing the loop using arrays in your program, and we'll help if you have problems.
Was This Post Helpful? 0
  • +
  • -

#5 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 07:51 AM

this is how I did the array


package lab2;

/**
 *
 * @author kyriakhskiathitis
 */
import java.util.Scanner;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner keyboard= new Scanner(System.in);
        
        System.out.println ("How many sentences are you inputing?");
        int answer=keyboard.nextInt();

        int[] answerArray=new int[answer];


       System.out.println("Please enter your sentences");
        String first=keyboard.nextLine();
       
      


        first = new StringBuffer(first).reverse().toString();
		System.out.println("String before reverse: "+first);
        





    }

}



Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 07:53 AM

The user will be inputting sentences. Do you want to store sentences in an int array?
Was This Post Helpful? 0
  • +
  • -

#7 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 07:59 AM

ok I used your sites and got this


package lab2;

/**
 *
 * @author kyriakhskiathitis
 */
import java.util.Scanner;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner keyboard= new Scanner(System.in);
        
        System.out.println ("How many sentences are you inputing?");
        int answer=keyboard.nextInt();

        int[] numbers=new int[answer];
for (int count=1; count <=answer.lenght; count++)
    
{
System.out.println(count + " "+ sentences);
}

       System.out.println("Please enter your sentences");
        String sentences=keyboard.nextLine();
      
        sentences = new StringBuffer(sentences).reverse().toString();
		System.out.println("String before reverse: "+sentences);
   
    }

}




Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:08 AM

That's a start. You have a few errors, and the purpose of your for loop isn't clear. You'll want to do something like:

for ( int count = 1; count <= answer; count++ )
{
    System.out.println( "Enter sentence " + count + ":  " );

    // then you could reverse the sentence and tell the user:

    System.out.println( "Your sentence reversed is: " + reverseSentence( input) );

    // etc . . . do the next sentence until done
}

Was This Post Helpful? 0
  • +
  • -

#9 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:17 AM

ok I go the counting thing done..thanks for that but it wont let me enter the sentences now ..the program runs but I can not enter my sentences....this is what I got



package lab2;

/**
 *
 * @author kyriakhskiathitis
 */
import java.util.Scanner;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner keyboard= new Scanner(System.in);
        
        System.out.println ("How many sentences are you inputing?");
        int answer=keyboard.nextInt();

        System.out.println("Please enter your sentences");
        String sentences=keyboard.nextLine();

        {
      
        sentences = new StringBuffer(sentences).reverse().toString();
		System.out.println("String before reverse: "+sentences);

        String[] numbers=new String[answer];

        for (int count=1; count <=answer; count++)

             {
             System.out.println("Line" + count + " :" );
              }

      
   
    }

}
}




Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:23 AM

Explain what you mean by "won't let me enter."

But if the user want to enter a number of sentences, you'll write the program to get the user's input INSIDE a loop that runs that number of times, not before the loop.

The structure is

declare, initialize variables
ask user how many sentences
start loop
get sentence
reverse sentence
output sentence
repeat loop until done
end loop
end program
Was This Post Helpful? 0
  • +
  • -

#11 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:28 AM

View PostGregBrannon, on 17 September 2011 - 08:23 AM, said:

Explain what you mean by "won't let me enter."

But if the user want to enter a number of sentences, you'll write the program to get the user's input INSIDE a loop that runs that number of times, not before the loop.

The structure is

declare, initialize variables
ask user how many sentences
start loop
get sentence
reverse sentence
output sentence
repeat loop until done
end loop
end program





I cant type in my sentences. It asks my first question. I then can enter the number of sentences I would like to run. Then the program just runs like this :
How many sentences are you inputing?
3
Please enter your sentences
String before reverse:
Line1 :
Line2 :
Line3 :
BUILD SUCCESSFUL (total time: 1 second)
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,786
  • Joined: 27-December 08

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:30 AM

The way Scanner works is this. When you use nextLine(), it reads in the remainder of the line and goes to the next line. The next(), nextInt(), etc., methods just read in the next token but do NOT go to the next line. So after nextInt(), you should invoke nextLine(). Then for your next prompt, nextLine() will read in your sentence.
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:31 AM

What Mac said PLUS fix your program structure.
Was This Post Helpful? 0
  • +
  • -

#14 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:42 AM


package lab2;

/**
 *
 * @author kyriakhskiathitis
 */
import java.util.Scanner;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner keyboard= new Scanner(System.in);
        
        System.out.println ("How many sentences are you inputing?");
        int answer=keyboard.nextInt();

        System.out.println("Please enter your sentences");
        String sentences=keyboard.nextLine();

        
      
        sentences = new StringBuffer(sentences).reverse().toString();
		

        String[] numbers=new String[answer];

        for (int count=1; count <=answer; count++)

             {
             System.out.println("Line" + count + " :" );
              }

      
   
    }

}

but i have the nextLine reader right before the string gets reversed... 



Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,786
  • Joined: 27-December 08

Re: must take user input and reverse each sentence that they write

Posted 17 September 2011 - 08:42 AM

And your question is? Are you encountering any problems or errors?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3