Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

simple questions in my code

 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below is supposed to look for the first occurrence of duplicate characters in a string.  For  example in "We like Pizza"  , it should return 11 and possibly even the letter z.

I attempted to write the code and I ran into many difficulties.  I have noted some of them in comments.  Ill list them here as well.  

1) how do I pass the value into the args of the Main Class?  Would this be done by calling it from a command line?  
2)  Why is there a red line under String strPieceA;?
3) Why is there an error on   strPieceB = "";  ?
4)  If strPieceA = strPieceB   it shows as a red underline error.   I also used:    if strPieceA.equals(strPieceB) and it showed as an error as well.  Maybe I need () around the two variables in the If.
5) How can I jump out of a For loop when a condition is met?   Do I need to use a While loop with a counter inside instead?

There are more errors but they seem to be repeats.


 
Carey Brown
Saloon Keeper
Posts: 8920
76
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
strPieceA = strInput.charAt(x);
You can't do this because strPieceA is of type String while the return value from charAt() is a char.
 
Piet Souris
Bartender
Posts: 4731
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quite some errors indeed, Another one:

Should be: ??? intLen;
 
Piet Souris
Bartender
Posts: 4731
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5) How can I jump out of a For loop when a condition is met?   Do I need to use a While loop with a counter inside instead?

You can use a while with a suitable flag, or use the 'break' command in the body of the for-loop.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If strPieceA = strPieceB
The 'if' requires parenthesis.
'=' is an assignment operator and '==' is the comparison operator.
 
Liutauras Vilda
Marshal
Posts: 8198
585
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of pointing out to all the issues this code has, I want to give you a suggestion for the future.

Do not write any extra code past the moment you figured there is something wrong - STOP right away and fix it, and only then consider moving forward.
Any other action in such case would be pretty much a waste of time, and the risk to make things even more worse.

Some style suggestion

This ceremony could and should actually be replaced with:

Two things here.

One - noticed how I got declaration and variable assignment on the same line, there is no point in having on two lines, so don't make it harder for the code reader (mostly you for now) than it really should.

Second - note how variable name from strInput I replaced with input, you don't really need to prepend/append variable name with it's type
information, that is specified already along with its declaration (String input = ...). Think that variable names are like a words in a book, they supposed to make a sense and be from a basic spoken language, only with some exceptions when we talk about loop counter indices, but that's pretty much it.

And actually third, equally important. Did you notice? I got rid off comment without losing any information. If you find situations that you need to explain what the particular line of code does, that's a sign that something won't be clear to the reader - and you don't solve that with a comment, but rather either making a variable name clearer so it is better communicates its intention. If there are few lines of code, equally you wrap them in a method, so it tells you what those 4-5 lines of code do.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:strPieceA = strInput.charAt(x);
You can't do this because strPieceA is of type String while the return value from charAt() is a char.



Carey,  
Appreciated info.  
I will change the type to char,  change the variable name from strInput to plain old input and do this all on one line.  
Kevin
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Quite some errors indeed, Another one:

Should be: ??? intLen;



Piet,
I appreciate your assistance.  

should it be declared like this?:

integer LengthOfInputString = 0:      

Kevin

 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no such type as "integer". There are the types "int" and "Integer" though.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:If strPieceA = strPieceB
The 'if' requires parenthesis.
'=' is an assignment operator and '==' is the comparison operator.



Carey,
I'll work on it using the == and put it in parenthesis.  Later I'd like to change it to the .equals()   method to learn about it.
Best,
Kevin
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
".equals()" only works for Objects and you don't have Objects you've got two chars.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Instead of pointing out to all the issues this code has, I want to give you a suggestion for the future.

Do not write any extra code past the moment you figured there is something wrong - STOP right away and fix it, and only then consider moving forward.
Any other action in such case would be pretty much a waste of time, and the risk to make things even more worse.

Some style suggestion

This ceremony could and should actually be replaced with:

Two things here.

One - noticed how I got declaration and variable assignment on the same line, there is no point in having on two lines, so don't make it harder for the code reader (mostly you for now) than it really should.

Second - note how variable name from strInput I replaced with input, you don't really need to prepend/append variable name with it's type
information, that is specified already along with its declaration (String input = ...). Think that variable names are like a words in a book, they supposed to make a sense and be from a basic spoken language, only with some exceptions when we talk about loop counter indices, but that's pretty much it.

And actually third, equally important. Did you notice? I got rid off comment without losing any information. If you find situations that you need to explain what the particular line of code does, that's a sign that something won't be clear to the reader - and you don't solve that with a comment, but rather either making a variable name clearer so it is better communicates its intention. If there are few lines of code, equally you wrap them in a method, so it tells you what those 4-5 lines of code do.



I appreciate all three of your suggestions and will use them.    
I used dBase code and then Baby VBScript  for a long time and I got in the habit of using a prefix to indicate the datatype.   I have to get away from that.  I will also get the declarations on to a single line.
I'll pull out the comments that don't assist the person reading the code or make it worse.

Best,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:".equals()" only works for Objects and you don't have Objects you've got two chars.


As mentioned, charAt() returns a type "char", so the variable name "strPieceA" no longer fits.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:There's no such type as "integer". There are the types "int" and "Integer" though.



I googled the difference between int and Integer.  I'll go with Integer because it is a wrapper of int and it mentions it can give more flexibility.  I'll find out how that is of value in the future.

Best,

Kevin
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:".equals()" only works for Objects and you don't have Objects you've got two chars.


As mentioned, charAt() returns a type "char", so the variable name "strPieceA" no longer fits.



Good to know.  I would have tried it and then had to ask the next question.  

Best,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables fall into two groups: "Objects", those things that inherit from the Object class, and built-in types, such as int. Built in types have the advantage of low overhead and are preferred unless you specifically need the behavior of an Object, such as being able to get a reference to it.

EDIT:
Class names ALWAYS start with an upper-case letter, that's how you can tell if you are dealing with an "Object".
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have to watch your character case a little more closely. Java is case sensitive, so "If" is not the same as "if". (The second one is the correct one.)
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Instead of pointing out to all the issues this code has, I want to give you a suggestion for the future.

Do not write any extra code past the moment you figured there is something wrong - STOP right away and fix it, and only then consider moving forward.
Any other action in such case would be pretty much a waste of time, and the risk to make things even more worse.

I have many places in my code where I am stuck.   I will take your advice and stop at the first blockage.  So here goes:

 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,
I appreciate you fixing the quote tag for me.  
I'm having a lot of simple code difficulties.  I first do some research on my questions but I still cannot figure out what I did wrong or how to fix each thing.  
I expect to struggle a while but I will get better.
I'm hoping the community will keep advising me until I can assist others.  
Kevin.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't actually need to assign anything here because when you call charAt() it will be filled in.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You don't actually need to assign anything here because when you call charAt() it will be filled in.



Would it be something like this?   Are you saying that I don't have to declare the type of PieceA.   Java will do it for me?   It doesn't seem right.  I don't know.



I appreciate your patience.  

W3schools is showing an example as:



I think that I do have to declare the type as char on the left side of the equals.

Thanks,

Kevin

 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could switch to using a While loop but I want to know if brake or continue are allowed in a for loop.  Most places writing about this say no.  A few places say yes.  I'd like to know if I am using the break/continue wrong or if it is not part of Java.

 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The posted code should have an error on the word: For
The spelling of for starts with a lowercase letter.

Where is the variable x declared?  The posted code does not show it.
Normally the loop iterating variable is declared in the for statement:

You can not do a continue immediately after a break and visa versa.  Either statement exits the current line of execution.
Do one or the other.
In the current code a continue would be redundant because it is at the end of the loop and would not change the execution.
A break at that location makes sense.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kevin Abel,
I have merged your topic into this topic. I hope that helps.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:

In this code you declare and initialize variable PieceA as a variable local to the for() loop. This is correct, the world outside of the for() doesn't need to know about this variable. So, remove any mention of  PieceA that is outside of this for() loop. Ditto for PieceB.

Note that ALL variables MUST begin with a  lower case letter. Please go through your code and fix.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that if you give your code the test input of "abcdef" it will throw an exception because of how you've structured the for() loop limit and the use of index 'x + 1'.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:kevin Abel,
I have merged your topic into this topic. I hope that helps.



I started a new thread because I didn't know if it was OK to have one gigantic thread.   Since it OK I am glad that you merged the threads into this one.

I will keep it going until I get the code working and also stop learning on the example.

I'll keep the code handy for interviews.  It is a great starting point for when they ask for a code example using strings.    

Thanks,

Kevin
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code working.   Did I miss any suggestions?
I learned A LOT!!


}
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 17 & 18 not needed at all.

Then on lines 25 & 26 put 'char' in front of them, which makes them local variables.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Give it an input string that has  NO repeating letters and see if your "no repeats" message appears.
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Lines 17 & 18 not needed at all.

Then on lines 25 & 26 put 'char' in front of them, which makes them local variables.



This worked.  I don't understand why it is OK to have the "char" in front of the variable.  It is in the "for" loop.   I would have expected an error saying that the variable has already been defined after the first pass.

Thanks,

Kevin
 
kevin Abel
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read through the thread 2x and my eyes are not finding the place where it was recommended that pieceA and pieceB are only used inside of the "for" loop.  It said something about if variables are only inside of loop they wont be seen outside of loop.   I'm not sure about how that rule works.  

I will remove some unused uncommented code.  Here is the code so far:

 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Space for the two variables is allocated in the heap at the time the method is first entered, however, the value will be random junk until the first statement is encountered that assigns to it. Each time around the loop, in your code, it gets initialized to a new value. Putting it inside the for() loop limits its "scope" to just the for() loop or anything inside the for() loop. Once the code exits the loop the variable will no longer be visible.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your "flagFoundIt" variable would have been better implemented as a boolean - more efficient. Then your if() statement could be
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:. . . if variables are only inside of loop they wont be seen outside of loop. . . .

Those are the usual rules about scope of variables. If you declare those two variables inside the loop, they cease to exist at the end of the loop.

flagFoundIt  . . .

That isn't a good name for a variable; if you said something like duplicateFound, it would be much easier to read.If something() changes the boolean, that will terminate the loop whenever you find a duplicate.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Space for the two variables is allocated in the heap at the time the method is first entered,


The variables local to a method could be allocated on the stack instead of the heap.  So when the method is exited, all the stuff on the stack for that method is popped off the stack and is gone.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic