Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Program printing more than once. Scanner problem.

$
0
0
Hello, i'm trying to create a program the takes user input and places it in an arraylist then prints the text backwards but if I enter more than 1 word the program prints the prompt to enter text more than once. Any help would be appreciated.

import java.util.ArrayList;
import java.util.Scanner;

public class ReverseUserDataArrayList 
{

	public static void main(String[] args) 
	{
		ArrayList<String> text = new ArrayList<String>();
		
		Scanner scan = new Scanner(System.in);	
		
		String reverseText = "";
		String input;
		int count = 0;
		
		do
		{
			if (count == 0)
			{
				System.out.print("Enter text (Type Q to exit): ");
				input = scan.next();
			}
			else
			{
                                //This prints once for every word input on one line
                                //I can't figure out how to stop it
				System.out.print("Line " + (count+1) + ": ");
				input = scan.next();
			}
			
			if (!input.equalsIgnoreCase("q"))
			{
				text.add(input);	
			}
			else
			{
				System.out.println("Input closed");
			}
			count++;
			
		}while (!input.equalsIgnoreCase("q"));
		
		
        scan.close();
                
        System.out.print("\nOriginal text: ");
        
        for (String userString : text)
        {
        	System.out.print(userString + " ");
        }
        
        System.out.print("\nReversed text: ");
        
        for(int i = text.size()-1 ; i >= 0 ; i--)
        {

        	reverseText += text.get(i);
        	reverseText += (" ");
        }
        
        System.out.println(reverseText);
	}

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>