• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

trying to finish this program using methods and arrays

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in need of help trying to figure out this java program. My program will ask the user to enter a sentence then another word. Then it will ask the user to pick a number and that will tell the user what character is in that position. In the main method the program puts the string into an array but i am suppose to make a new method to find out what character is in the position they asked for. I have been using bluej for where i do my programming. Any help will be much appreciated. thank you very much. Here is my program i tried to use a .java file to attach but it wont let me.


this is the part im having problems with
 
Marshal
Posts: 70223
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You didn't quite get the code tags right, but don't worry; I have sorted them out. You can find out about the tags here.
Your method says it returns a char but you are returning a char array from inside the loop. That won't work.
 
Campbell Ritchie
Marshal
Posts: 70223
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you don't need that loop in line 39.
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the code comments, I guess you aren't allowed to just call

I found this the most frustrating part of one of my CS courses back in the day (wow, saying that makes me feel old...). Why not just use the libraries you have to work with? Makes no sense...[/rant]

Anyway, to the problem at hand. As Campbell pointed out, you are trying to return a char[] inside the loop when your method declares (correctly) that it is returning a char. Hint: you passed in pos. What should you do with it?

One other thing I noticed is here:


The first call should be using charSentence, no?

It's also generally (arguably?) not a good idea to reassign variables like that. It's quite easy to run into trouble with reasoning about the state of things in more complicated programs when you let variables mutate like that. It's better to name your variables more precisely, or eliminate them altogether if you can inline the call.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic