Jump to content

string tokenizer

- - - - -

  • Please log in to reply
10 replies to this topic

#1
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
i need to create a program that reads a 3 word sentence from a user. each word is separated by 1 or more spaces.
the program then changes the first and last words. ( for example I am here will change to here am I)
if the user does not enter 3 words then the program ends.



an example of how it should be

please enter three words separated by one or more blanks:>  you     will code

the new sentence is : code will you



please enter three words seperated by one or more blanks:>you  will

Error: There are not three words in the sentence

Exiting Program..



please enter three words seperated by one or more blanks:>you

Error: There are not three words in the sentence

Exiting Program..



please enter three words seperated by one or more blanks:>does   it      work

the new sentence is : work does it



I was thinking of using stringtokenizer to do this, the thing is i do not know how to use stringtokenizer.
could i use split sting?
if i can then how would i?

how could i read the 3 words and store each word in an array? then print the array later?

Edited by pindo, 04 May 2011 - 05:52 AM.


#2
Tejal

Tejal

    Newbie

  • Members
  • Pip
  • 9 posts

pindo said:

I was thinking of using stringtokenizer to do this
I would advice to use the Scanner instead of tokenizer.

1) Use Scanner to read the user input.

2) Store it in a String.

3) Then use the Split method of String and store the input in the String array.

4) If the array length is 3 which is the number of input Strings, then manipulate the array and replace the elements.

5) If the length is not exactly equal to 3 then exit with proper message.

That would be enough. :cool:

Tejal


#3
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts

	public void Print() {

		String Input;

		Screen.out.println("enter 3 words:");

		Input= Keybd.in.readWord();

		String str = Input;

		String[] words = str.split (" ");

Screen.out.println and Keybd.in.readLine(); have been given as a package to me by my uni.

i do not know how to use splitstring
could you give an example of each and ill change this to work for mine.

#4
Tejal

Tejal

    Newbie

  • Members
  • Pip
  • 9 posts
Can you show the whole code that you have tried so far instead of showing a piece in between?

Tejal


#5
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts

import B102.*;



public class SwitchString {


	public static void main(String[] args) {


		String input;

		Screen.out.println("enter 3 words:");

		input= Keybd.in.readLine();

		String str = input;

		int [] input = new int [3];

		String[] words = str.split (" ");

		

	}


}



b102 allows me to use Keybd.in.readLine and also Scree.out.println()

im so lost
im very new to java

#6
Tejal

Tejal

    Newbie

  • Members
  • Pip
  • 9 posts

pindo said:


import B102.*;



public class SwitchString {


	public static void main(String[] args) {


		String [COLOR="red"]input[/COLOR];

		Screen.out.println("enter 3 words:");

		[COLOR="red"]input[/COLOR]= Keybd.in.readLine();

		String str = input; [COLOR="blue"]// You don't need to assign Strings again n again.

                // Use the input String declared earlier. Delete this string.[/COLOR]

		int [] input = new int [3]; [COLOR="blue"]//You don't need this array. Delete it.[/COLOR]

		String[] words = [COLOR="red"]input[/COLOR].split (" ");

                [COLOR="blue"]// Now, validate your words array's length here. 

                // If it's 3, re-arrange the elements inside the array and display them 

                // OR exit.[/COLOR]

	}


}

See my comments above.

I am just guessing that your customized package is providing the standard operations like standard java println() and redLine() of Scanners.

Tejal


#7
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
how do i validate arry lengths?
how do i re arange array elements?

i dont know anything :/

#8
Tejal

Tejal

    Newbie

  • Members
  • Pip
  • 9 posts

pindo said:

how do i validate arry lengths?
There is a property of Arrays called length that gives you number of elements in the array. See following,

String[] arr = new String[3];

System.out.println("Array Length : " +arr.length);

// Which prints, "Array Length : 3"

pindo said:

how do i re arange array elements?
Store the 1st element in a String. Replace the 1st element with the 3rd and replace the 3rd element with the one stored in the earlier String. Something like,

String a1 = arr[0];

arr[0] = arr[2];

arr[2] = a1;

I hope that's enough explanation.

[Note: Arrays start with 0 index. Hence the 3rd element comes at index 2] :cool:

Tejal


#9
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts


import B102.*;



public class SwitchString {


	public static void main(String[] args) {


		String input;

		Screen.out.println("enter 3 words:");

		input= Keybd.in.readLine();

		

		String[] word = input.split (" ");

		String[] words= new String[3];

		System.out.println("Array Length : " +words.length);

		input = word[0];

		word[0] = word[2];

		word[2] = input;

		Screen.out.println("Your new sentence is: " +word[0]+" "+word[1]+" " + word[2]+" ");


	}

}


that works great

but now if i enter one letter i need it to print exit and then it quits. how do i do that?
also the program does not take multiple spaces. how do i allow this to take multiple spaces?

#10
Tejal

Tejal

    Newbie

  • Members
  • Pip
  • 9 posts
You don't need to have that String array called words. The previous String array word is enough to store the input values.

Plus, use a new String other than input to store the Array element for better approach.


pindo said:

but now if i enter one letter i need it to print exit and then it quits. how do i do that?
That's what I meant to say by validation your array element lengths. Use the if/else statements to manipulate the things.

if(arrayLength == 3){

// Replace the array elements

}

else{

//Exit.

}

Hope you are clear enough,

Tejal


#11
Tejal

Tejal

    Newbie

  • Members
  • Pip
  • 9 posts
Pindo, Any luck in your assignment? :sneaky:

Tejal





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users