• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

inputs should be in same line

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hello i have exercise "Write a program that takes a string and two characters from the standard input and then prints the number of time the two characters occur consecutively in the string (in any order)."
well i already solve.But i have a problem with input.
Input data format
The single input line contains a string and two characters.
according to solution input pattern should be
abcd b c       OR bcbc c b
1                       3
But i cannot enter my input like that. i had to do;
abcd     OR bcbc
b c            c b
and i get Runtime error.i cannot enter input and two char in a same line
 
Saloon Keeper
Posts: 9564
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

umit morris wrote:The single input line contains a string and two characters.
according to solution input pattern should be
abcd b c       OR bcbc c b
1                       3
But i cannot enter my input like that. i had to do;
abcd     OR bcbc
b c            c b
and i get Runtime error.i cannot enter input and two char in a same line


I take issue with the requirements. A String may contain spaces, yet their solution depends on there being no spaces.
 
umit morris
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah but if we assume no whitespace in string how to be input string and two character input next to each other,in java is that possible as their pattern?
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the requirement is to enter the entire String first ("abcd b c" without the quotes), then you'll have to get that one String from the Scanner and break it up into the parts you need.  You won't be able to use three calls to the Scanner methods, just one.
 
umit morris
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:If the requirement is to enter the entire String first ("abcd b c" without the quotes), then you'll have to get that one String from the Scanner and break it up into the parts you need.  You won't be able to use three calls to the Scanner methods, just one.




i did it by splitting thank you.Maybe someone will use.Of course, maybe a shorter way.
 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's how I was thinking of doing it.  A couple of suggestions on your code:

* Your formatting is okay, but could improve.  See HowToFormatCode (that's a link).
* Even though it doesn't really matter in a tiny program like this, you should get used to using variable names that mean something.  "n", "k", "l" don't mean much.
* There is no error checking.  I don't know if that is a part of the requirements, but entering a String that has no spaces or nothing between spaces will cause errors.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic