Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find second word in a string without using methods?

 
Harivenkatesh Polnati
Greenhorn
Posts: 25
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hari

Always use code tags.



Welcome to the ranch.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Tony Docherty
Bartender
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49788
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be more useful to find out who suggested you do such things without using methods … and beware of that person’s advice
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic