10 Replies - 6414 Views - Last Post: 15 October 2011 - 07:45 PM Rate Topic: -----

#1 heavenvein  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 14-October 11

get words in string when user enter number and sentence

Posted 14 October 2011 - 08:59 PM

Write a program with a single main method to prompt the user for an integer i and a sentence. The program prints the ith word of the sentence. Words in the sentence are assumed to be zero-origin. The user is then prompted for another integer and sentence. If the empty sentence is entered (i.e., a sentence with no words), the program exits.
For example, if user input is “1” and “Tom threw the ball” the program output is
threw
i have been spending hours to find what code can i get the words from sentence when i input the number and sentence. i think i am getting stuck at while(a.hasMoreToken()). Please give me some idea. thank you.

import java.util.Scanner;
import java.util.StringTokenizer;

public class Problem1C
{
	/**
	 * @param args
	 */
    public static void main(String[] args) 
    {
    	boolean quit = true;
        Scanner input = new Scanner(System.in);
     
        while(quit)
        {
        	System.out.print("Enter integer number: ");
        	String i = input.nextLine();
        	System.out.print("Enter string: ");
            String st = input.nextLine();
           // int count=0;
            StringTokenizer a = new StringTokenizer(st);
            
            while(a.hasMoreTokens())
            {
            	
            	
            }
            
            while(st.equals(""))
            {
            	quit = false;
            }  
        } 
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: get words in string when user enter number and sentence

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: get words in string when user enter number and sentence

Posted 14 October 2011 - 09:06 PM

Try string.split

Split the sentence on the space character this will return a string array.
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: get words in string when user enter number and sentence

Posted 14 October 2011 - 09:09 PM

The StrinkTokenizer class is almost deprecated
use the String.split(" ") method, applied to scan.nextLine(), instead it will be a lot simpler and cleaner
Was This Post Helpful? 2
  • +
  • -

#4 heavenvein  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 14-October 11

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 09:43 AM

i still get problem that when i want to quit, then the system get error.

/**
 * This program will get the words from string which input by users.
 */
import java.util.Scanner;

public class Problem1C
{
	/**
	 * @param args
	 */
    public static void main(String[] args) 
    {
    	boolean quit = true;
        Scanner input = new Scanner(System.in);
     
        while(quit)
        {
        	System.out.print("Enter integer number: ");
        	String i = input.nextLine();
        	System.out.print("Enter string: ");
            String st = input.nextLine();
            
            String[] words = st.split(" ");
            int j = Integer.parseInt(i);
        	System.out.println( words[j]);
          
            while(st.equals(""))
            {
            	quit = false;
            }  
        } 
    }
}
[/cpde]

[quote name='heavenvein' date='15 October 2011 - 09:39 AM' timestamp='1318696765' post='1461179']
i still get problem that when i want to quit, then the system get error.

[code]
/**
 * This program will get the words from string which input by users.
 */
import java.util.Scanner;

public class Problem1C
{
	/**
	 * @param args
	 */
    public static void main(String[] args) 
    {
    	boolean quit = true;
        Scanner input = new Scanner(System.in);
     
        while(quit)
        {
        	System.out.print("Enter integer number: ");
        	String i = input.nextLine();
        	System.out.print("Enter string: ");
            String st = input.nextLine();
            
            String[] words = st.split(" ");
            int j = Integer.parseInt(i);
        	System.out.println( words[j]);
          
            while(st.equals(""))
            {
            	quit = false;
            }  
        } 
    }
}


[/quote]
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 10:05 AM

What error specifically - on which line?


See FAQ #5 for how to debug your own applications


FAQ (Frequently Asked Questions - Updated Sep 2011

Spoiler


Why do you have this wrapped in a While construct that serves no purpose? Since you are going running this one time you can do away with the While block entirely.

You do realize that arrays are zero indexed - right?
So element 0 is the first item. Element 1 is the second item. So if you have 10 items they are 0-9. If you try to fetch element 10 it will fail because you don't have 11 items.
Was This Post Helpful? 0
  • +
  • -

#6 heavenvein  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 14-October 11

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 10:50 AM

the purpose: If the empty sentence is entered (i.e., a sentence with no words), the program exits. so i can input many times i want, and if i stop it, i will input no words and it will be quit. i think the the error line is 27.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10805
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 11:08 AM

And the error is?
Was This Post Helpful? 0
  • +
  • -

#8 heavenvein  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 14-October 11

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 11:12 AM

when i enter space to quit the program, Esclipse show: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Problem1C.main(Problem1C.java:25)

how dont know how to build on my program to quit. :(
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 12:36 PM

If the sentence is "hello world" which has 2 words and the user enter 5 you have to check for the array bound


   int j = Integer.parseInt(i) - 1;  // -1 arrays goes from 0 to length-1
   if(j < 0 || j >= words.length)
       System.out.println("J should be between 1 and " + words.length);
   else
       System.out.println( words[j]);


Was This Post Helpful? 1
  • +
  • -

#10 heavenvein  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 14-October 11

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 01:27 PM

thank you. i got that one. however, If the empty sentence is entered (i.e., a sentence with no words), the program exits. i am in trouble with the quitting program. that why i am using "while" to quit program, but it is still run into error.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: get words in string when user enter number and sentence

Posted 15 October 2011 - 07:45 PM

you can always


while(true)
        {
            System.out.print("Enter string: ");
            String st = input.nextLine();
            if(st.length() == 0)
                  break;
            System.out.print("Enter integer number: ");
            String i = input.nextLine();


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1