Results 1 to 7 of 7

Thread: need help

  1. #1
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default need help

    PROBLEM:Ask user to enter the rollnumber or name of the student.
    1.Check whether the roll number starts with IAD then search the array and display the marks associated with the rollnumber .
    2.If u have enter the name then check whether it has only characters and not numbers and search the array to display the marks associated with the name,if the rollnumber is not found then display message saying no data found.
    3.Else ask user to enter the rollnumber or name of the student again.

    Here is the code which i have written.


    Java Code:
    for (;;) {
                  System.out.print("Enter the rollnumber or name of the student result:");
                  result = scan.next();
                  if (rollnumber.startsWith("IAD")) {
                  System.out.println("Roll number starts with IAD");
                  } else {
                           break;
                            }
                  for(rowindex =0;rowindex<rs;rowindex++){
                      if(rollnumber.equals(arr[rowindex][0])){
                          // found = true;
                           System.out.println(arr[rowindex][2]);
                     
                      if(found == false){
                        System.out.println("data not found");
                     }
                  
                      } else if  (isNumber(studentname)) {
                        continue;
                    } else {
    
                        break;
                    }
                   for(rowindex =0;rowindex<rs;rowindex++){
                       if(studentname.equals(arr[rowindex][1])){
                          
                            System.out.println(arr[rowindex][2]);
                             break;
                       }
    
                  else
                      continue;
               
                  }
              }
        }
    }
    }
    Can anyone help me with the code.I am not getting the correct output and i feel that i am not closing the loops properly .
    Thank you all for your help in advance

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,016
    Rep Power
    10

    Default

    What output are you getting instead? What happened when you stepped through it with a debugger? And why do you "feel" that you are not closing the loops properly?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi, for these cases should be used while loop
    for example
    Java Code:
    boolean isExit = false;
    while (!isExit) {
        if (your condition exit of loop) {
            isExit = true;
        }
    }
    Skype: petrarsentev
    http://TrackStudio.com

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,016
    Rep Power
    10

    Default

    Quote Originally Posted by Petr View Post
    Hi, for these cases should be used while loop
    Stylistically that may be true, but as far as semantics go, it shouldn't really matter. You should be able to achieve the same thing with a for(;;) and a break. I would think the OP should fix his logical errors before commenting on his stylistic errors.

    OP- Fixing your indentation will probably help you figure out when your loops and if statements are completing.
    Last edited by KevinWorkman; 01-25-2011 at 04:44 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    for (;;) {
         COMES BACK HERE AGAIN         System.out.print("Enter the rollnumber or name of the student result:");
                  result = scan.next();
                  if (rollnumber.startsWith("IAD")) {
                  System.out.println("Roll number starts with IAD");
                  } else {
                           break;
                            }
                  for(rowindex =0;rowindex<rs;rowindex++){
                      if(rollnumber.equals(arr[rowindex][0])){----EXECUTES TILL HERE AND DOESN'T PRINT  System.out.println(arr[rowindex][2]);
                          // found = true;
                           System.out.println(arr[rowindex][2]);
                     
                      if(found == false){
                        System.out.println("data not found");
                     }
                  
                   COMES HERE   } else if  (isNumber(studentname)) {
                        continue;
                    } else {
    
                        break;
                    }
                   for(rowindex =0;rowindex<rs;rowindex++){
                       if(studentname.equals(arr[rowindex][1])){
                          
                            System.out.println(arr[rowindex][2]);
                             break;
                       }
    
                  else
                      continue;
               
                  }
              }
        }
    }
    }

    WHEN I DEBUG THE OUTPUT I GET IS:
    Enter the rollnumber or name of the student result:IAD67
    Roll number starts with IAD
    Enter the rollnumber or name of the student result:

    PLEASE HELP ME IN SOLVING THIS PROBLEM

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Can you post full example or example, which we can run?
    Skype: petrarsentev
    http://TrackStudio.com

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,016
    Rep Power
    10

    Default

    You still haven't told us why that output is wrong. You haven't told us what you expected instead. You simply dumped your homework here and hoped for the best. That's not how this works.

    I didn't ask you what the debugger told you for our information. I asked you that to encourage you to trace through your program and determine when the actual behavior differed from your expected behavior, in the hopes that you would take it from there.

    As has been requested, if you want further help, you'll have to provide an SSCCE that demonstrates the problem, and you'll have to be more specific about what's happening why that's wrong, and what you expected instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •