This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String indexing  RSS feed

 
adebari olalekan
Ranch Hand
Posts: 49
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, this code has make puzzled since last week, and i finally think i have exhausted my knowledge.
what happened is that; i want this code to run as many times as the input testcase variable permit. but the way i wrote it, it seemed to be limited to the amount of times i can anticipate and modify the code to match that times.
this is not a good idea; i mean what is a user wants the code to run more than the times my testcase variable anticipated. am puzzled at how to handle the variables that will store the variables if the user enter more than i anticipated; unless i can have another idea. thanks in advance pals.


please someone should help. it doesnt give an error , but it will if it run more times than i expected. how do i handle this unknown contingency.
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that is some confused code, with doubtless all sorts of errors hiding in it. The only one I can see at the moment is that you are closing a Scanner pointing to System.in, which is a serious error, but probably not the cause of your current problem.
Your main method is much too long; the idea is one statement. The longer the method, and the more loops it has in, the harder it will be to debug. A main method that long can probably be divided into six or seven other methods.
I can suggest all sorts of other enhancements which I think you shou‍ld make, which will make it easier to correct that code.
  • 1: Give all the variables clear names. Things like mychar or d are not clear.
  • 2: Consider whether all those variables are needed. Can you replace mychar with direct use of myText.toCharArray()? Can you replace mychar.length with myText.length()?
  • I suggest you need to divide each loop into a method by itself. As you go through each loop, print the indices and the lengths of the StringBuilders you have in those methods. that will give you some idea where things are going wrong.
     
    Liutauras Vilda
    Marshal
    Posts: 4261
    256
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    char odd2;

    What Campbell mentioned about confusing variable names, odd2 is confusing, because 2 isn't odd, and what is the difference between odd and odd2? where is odd1?

    Probably issue is very obvious once you add clarity to your code.
    If you were to create a method, might you'd be able to remove half of the code, because those loops look repetitive and almost identical.
     
    adebari olalekan
    Ranch Hand
    Posts: 49
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    guys! here i have reshape the code ; but even after using methods as a template to work on the inputs; the problem still persists. the code cannot run more than once ; and if the user enter more than
    that ,it will start duplicating .. kindly help please.

     
    Carey Brown
    Bartender
    Posts: 2700
    41
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    adebari olalekan wrote:

    You already have the array inputedStrings1, why add evendetector?
     
    Carey Brown
    Bartender
    Posts: 2700
    41
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    It's not clear to me why a testcase involves two inputs.
     
    adebari olalekan
    Ranch Hand
    Posts: 49
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:
    It's not clear to me why a testcase involves two inputs.



    test case involves two because that is the only option that the codes works with. it does not works when the test case have to be up to 3,4,5 or greater, and that is the problem i am facing; how to make the code make an array of any input strings and then strip the array to get the odd and even index values and print it out to std-out ; and it wont work if the input is to be worked on simultaneously as it is inputted (i would have been through with it if that is the case) ,it will only works when the user enters the number of inputs he/she will be entering,  and then the code works on the inputs and then print it out.
    here is an example of how i want it to run;



     
    Henry Wong
    author
    Sheriff
    Posts: 23260
    124
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    The code, as written, expects two inputs. This is true, regardless, of the number that is entered by the user. Although, the program seems to use that number to run the same two inputs that number of times.

    Perhaps, after getting the number of values to input, you can use a loop to read the input into an array or list. And then, later, use the other loop to process the array or list (instead of processing the same two inputs)?

    Henry
     
    Carey Brown
    Bartender
    Posts: 2700
    41
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    adebari olalekan wrote:
    Carey Brown wrote:It's not clear to me why a testcase involves two inputs.
    test case involves two because that is the only option that the codes works with. it does not works when the test case have to be up to 3,4,5 or greater...

    Why not get it work for 1 first and then wrap a loop around that?
     
    adebari olalekan
    Ranch Hand
    Posts: 49
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks you very much pals. i appreciate the helps . i finally read the inputs into an Array and make the methods to run on each iteration of the inputs.
    thanks , i would have dropped (difficult)Java if not for this platform .
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is nothing difficult about Java®. It is programming that is difficult.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!