wood burning stoves*
The moose likes Beginning Java and the fly likes Find second word in a string without  using methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find second word in a string without  using methods?" Watch "Find second word in a string without  using methods?" New topic
Author

Find second word in a string without using methods?

Harivenkatesh Polnati
Greenhorn

Joined: Mar 17, 2012
Posts: 25

Hi,
How to find middle word in a given string without using methods?
I write one logic.

Is there any Better logic?

import java.util.Scanner;
public class SecondDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Your Statement");
String s=sc.nextLine();
for(int i=0;i<s.length();++i)
{
if(s.charAt(i)==' ')
{
for(int j=i+1;j<s.length;++j)
{

if(!(s.charAt(i)==' '))
{
System.out.print(s.charAt(i));
}
else
{
break;
}
}
}
break;
}
}
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi Hari

Always use code tags.



Welcome to the ranch.


~ Mansukh
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

ummm...

main() is a method.
println() is a method.
nextLine() is a method.
length() is a method.
charAt() is a method.

You see where I'm going with this?

so..what do you mean by "without using methods"?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2288
    
  49
How to find middle word in a given string without using methods?

You can't, and to prove the point your code is full of methods.

Edit: Just noticed Fred has beaten me to the punch.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
It would be more useful to find out who suggested you do such things without using methods … and beware of that person’s advice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find second word in a string without using methods?