• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

MOOC 15.1 Exercise 53

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exercise: https://materiaalit.github.io/2013-oo-programming/part1/week-3/
My code:


I don't know how to make program ask user about length of first part, I only know how to read specific part of sentence, halp.
 
Marshal
Posts: 76886
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what you are trying to do; many of us won't click a website like that, even GitHub.
Don't mix nextLine() and Integer#parseInt(). Use Scanner#nextInt(). Unfortunately you are trying to use nextLine() afterwards. As explained here, you will get an empty line from nextLine(). The simplest solution to that problem in the present instance would be to add a line 12½ reading reader.nextLine(); Nothing else.

What have you been taught about Scanner? I seem to find that many people aren't taught about it at all well.
 
Sheriff
Posts: 17357
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're mixing the solutions for two separate exercises. Don't do that because it makes your code messy and confusing.

Exercise 53 comes right after section 15.1 which explains how to use String.substring(). You're supposed to ask the user to enter a word, then enter a number signifying the length of the first part of that word that you want to extract. The examples given are pretty clear:

Type a word: example
Length of the first part: 4
Result: exam

Where the bold underlined things above are what the user would enter. So use Scanner.nextLine() to get the word and Scanner.nextInt() to get the "length of the first part". Then use String.substring() to get that many characters from the front of the word.
 
Campbell Ritchie
Marshal
Posts: 76886
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DR: I presume you know there are two overloaded versions of the substring() method: 1 2. Please check which of those will better serve your purposes.
 
His brain is the size of a cherry pit! About the size of this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic