• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find second word in a string without using methods?

 
Greenhorn
Posts: 25
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hari

Always use code tags.



Welcome to the ranch.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79240
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic