5 Replies - 19509 Views - Last Post: 27 January 2011 - 06:31 PM Rate Topic: -----

#1 nebulinda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-April 09

Read multiple words from keyboard

Post icon  Posted 21 April 2009 - 08:00 PM

I have to write a program which will read in content from the keyboard and then be printed to a file. The point of the assignment is learning how to write to files, and I have that part, but I don't know how to save more than the first word I type into a String variable.

Here's my code:

import java.util.*;

public class MemoPadTest {

	public static void main(String[] args) {

		String fileName = new String();
		String title = new String();
		String body = new String();

		Date date = new Date();
		String dateString = new String(date.toString());

		Scanner keyboard = new Scanner(System.in);

		System.out.print("Enter a file name: ");
		fileName = keyboard.next();
		System.out.println();

		System.out.print("Enter a title: ");
		title = keyboard.next();
		System.out.println();

		System.out.println("Enter a memo body:");
		body = keyboard.next();

		String[] memo = {dateString, title, body};

		MemoPad pad = new MemoPad(fileName);
		pad.writeMemo(memo);

	}

}



When entering a memo body, if I typed "This is a memo," the only word to be saved is "This." I thought about making each word an element in an array, but I don't know how to make a space indicate next element.

Help is appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Read multiple words from keyboard

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Read multiple words from keyboard

Posted 21 April 2009 - 09:24 PM

the function keyboard.next() will only read one token. Meaning if you entered "The Ocean" it would only take "The" - as a result you must read line using keyboard.nextLine();

You can then use StringTokenizer if you wish to break each line down into components depending on how you want to store/manipulate data

This post has been edited by bbq: 21 April 2009 - 09:24 PM

Was This Post Helpful? 1
  • +
  • -

#3 nebulinda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-April 09

Re: Read multiple words from keyboard

Posted 21 April 2009 - 09:50 PM

Thanks, that was helpful. But I'd like both the title and body to have multiple words. When I do
System.out.print("Enter a title: ");
		title = keyboard.nextLine();
		System.out.println();

		System.out.println("Enter a memo body:");
		body = keyboard.nextLine();

and run it, it skips the opportunity to enter the title and goes right to the body.

EDIT:
Actually, when I do body = keyboard.nextLine(); and leave title = keyboard.next(); as is, it still allows me to enter the title, but just skips entering the body.

And when I have title = keyboard.nextLine(); but leave body = keyboard.next(); it skips entering the title.

This post has been edited by nebulinda: 21 April 2009 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Read multiple words from keyboard

Posted 21 April 2009 - 10:58 PM

Weird perhaps try changing your first print statement to System.out.println("Enter a title: "); rather than System.out.print("Enter a title: " );

I am not 100% sure on this as i have not tested it, however perhaps its skipping it because you have print instead of println meaning it already has information to read in on that line as you do not start a new line with 0 characters.... Just try it and let me know how it goes :)
Was This Post Helpful? 0
  • +
  • -

#5 nebulinda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-April 09

Re: Read multiple words from keyboard

Posted 21 April 2009 - 11:14 PM

Your suggestion actually didn't do anything, so tried changing "fileName = keyboard.next();" to "fileName = keyboard.nextLine();" and it worked. I have no idea why that solved my problem, especially since I don't really want the file name to be more than one word. But that's not even an issue, so I'll take it.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Memophysic*


Reputation:

Re: Read multiple words from keyboard

Posted 27 January 2011 - 06:31 PM

I found a solution since I was experiencing the same problem. I know it's been a long time since the last reply, but if it can help some web surfers looking for an answer to their question, it'll be that at least.

Simply putting a scan.skip("\n") before the actual scan.nextLine() has solved the problem for me.

Code example to show you it does work:

import java.util.Scanner;

String text1, text2;

Scanner scan = new Scanner(System.in);

//Simple scanner for simple word.
System.out.print("Enter one word: ");
text1 = scan.next();

//Simple modified way for many words
System.out.print("Enter many words like a sentence: ");
scan.skip("\n");
text2 = scan.nextLine();

//Verify both scans were correctly done
System.out.println("text1: " + text1 + "\ntext2: " + text2);



For anybody it could help!
Was This Post Helpful? 1

Page 1 of 1