• Post Reply Bookmark Topic Watch Topic
  • New Topic

using if/else while 'looking for a character'  RSS feed

 
Andreas Eng
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
I am currently working a java course online and am having a lot of problems with this simple bit.
I can't seem to turn my mind around and think I did something wrong here.

Program:
The program is made so that it will print when a certain letter is found in an input line.

Problem:
I cannot seem to find a way to use the if condition to recognize that the letter has been found, and will print the specific line. The else statement will work as long as the if statement is false.

Here is my code:




Now tell me, lords of Java: What have I done wrong?

(I appreciate any kind of help and critique!)
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Always use the tags: doesn't it look better
Where does the variable tekst come from? I can see pos and I can see tesktLest but not tekst. Maybe you meant if (pos...
 
Andreas Eng
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
I added code tags to your post. Always use the tags: doesn't it look better
Where does the variable tekst come from? I can see pos and I can see tesktLest but not tekst. Maybe you meant if (pos...


Thanks for the welcome and thanks for adding code tags. I'll remember that next time
the variable tekst comes from a failed try earlier to find out if I could convert the string to an int. the int was called tekst.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Eng wrote:the variable tekst comes from a failed try earlier to find out if I could convert the string to an int. the int was called tekst.

It's kind of hard to tell you what you're doing wrong if you don't show us your actual code. All we know is what you post in this forum, so if you post something with code from " a failed try earlier", that's all we can critique. The code you posted won't work because tekst isn't declared in the code you posted.

So please post your actual code. Post the text of any compilation errors (no screenshot, please). Post what input you give. Post the actual output you get and what output you expected. To REALLY help you solve the problem, we need all that information.
 
Andreas Eng
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Andreas Eng wrote:the variable tekst comes from a failed try earlier to find out if I could convert the string to an int. the int was called tekst.

It's kind of hard to tell you what you're doing wrong if you don't show us your actual code. All we know is what you post in this forum, so if you post something with code from " a failed try earlier", that's all we can critique. The code you posted won't work because tekst isn't declared in the code you posted.

So please post your actual code. Post the text of any compilation errors (no screenshot, please). Post what input you give. Post the actual output you get and what output you expected. To REALLY help you solve the problem, we need all that information.


I do understand what you mean and it seems I haven't been able to express my questions properly.

What I want to do is:
1. Make it so that a user inputs a sentence.
2. That the program returns the placement of the first C in the sentence put in by the user.
- If there are no Cs in the sentence then return a message saying that there are no Cs in the sentence.


Now what I posted for, which I haven't expressed properly enough is:
I want and need help so solve this, not only correct whatever I may have done wrong already.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Eng wrote: . . . find out if I could convert the string to an int. the int was called tekst.
There are ways to find int values from Strings, but calling an int “tekst” is very confusing. I thought the name tekst means it is a String.
 
Andreas Eng
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do understand, but basically I was just shortening the tekstLest variable.
 
Liutauras Vilda
Sheriff
Posts: 4922
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Eng wrote:I do understand, but basically I was just shortening the tekstLest variable.
Hi, basically what you have been told already and some other things are:
a) variable names suppose to make sense and be descriptive. At least not confusing as variable "tekst..." which could be understood as a String. So choose something more clear.
b) not exception a class names too. Should be something meaningful. If it is an assignment and it is a requirement, then it is fine, but do remember that for the future.
c) your "main" method should not contain all program flow in it. Method "main" suppose to execute your program and nothing else - ideally 1 line of code.
d) 'C' is not equal 'c'. Java is a case-sensitive.

What's next:
1. you need probably to create a method for your task. If task is to print some text message and accept users input - create a method for that, and only for that.
2. after you got user's input, you'll need another method, which checks if any 'c' are in your String.
 
Liutauras Vilda
Sheriff
Posts: 4922
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And your program could look similar:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!