Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need some help with my code

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some help with my code

    Hey if someone could help me shed some light on my code as too why it is getting the error String index out of range: -1 i would be for ever in your debt

    the part of the question im stuck on is this
    "At the beginning, your program should ask the user to specify a one-to-one mapping between the
    characters in the above mentioned alphabet. For example, your program may display the 40 characters
    in one line and ask the user to type the same set of characters in the second line, but in a different
    order. After the mapping is established, your program should then ask the user to type in a sentence
    using the characters from this alphabet, and then output the encrypted sentence"


    The bit in red is the bit im really stuck on

    Code:

    package question3;

    import java.util.Scanner;

    public class Question3 {
    private String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ,.!";
    private String MAP = "";
    private String Pass = "";
    Scanner kb = new Scanner(System.in);

    public void getString(){

    System.out.println("These are the Characters that are able to be used");
    System.out.println(alpha);
    System.out.println("Please input encryption");
    MAP = kb.nextLine();
    MAP = MAP.toUpperCase();
    }

    public void checkalpha(){
    int firstIndex = 0;
    int lastIndex = 0;
    int count = 1;

    boolean repeatedChars = false;

    if(MAP.length() == 40){

    for (int i = 0; count < MAP.length(); i++){

    char character = MAP.charAt(i);
    firstIndex = MAP.indexOf(character);
    lastIndex = MAP.lastIndexOf(character);


    if(firstIndex != lastIndex) {
    repeatedChars = true;
    }
    count++;
    }

    if(repeatedChars == true) {
    System.out.println("There were repeated characters in the string");
    System.out.println("Please Do not Input the characters more than Once");
    }
    else {
    System.out.println("No Repeated Characters");
    }
    if (MAP.equals(alpha)){
    System.out.println("Please Do not repeat the starting characters");
    getString();

    }
    }
    else {
    System.out.println("Incorrect Amount of Characters");
    System.out.println("");
    }

    }

    public void printConverted(){

    char PassIndex;

    System.out.println("Please Input message wanted to be converted");
    Pass = kb.nextLine();
    for (int i = 0; i < Pass.length(); i++){

    PassIndex = MAP.charAt(Pass.indexOf(alpha.charAt(i)));
    System.out.println(PassIndex);
    }

    }

    printConverted method is the one with the problem, im positive about that.

    Thanks for taking a look, if you need more info about something please let me know, i am very new to java so i really want to learn what im doing wrong
    Last edited by cool_0; September 13th, 2012 at 08:07 AM. Reason: INFO:


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with my code

    DON'T WORRY! Cause I'm so Smart i was only entering lower case letters, and of course, it was only reading Capitals

    SMART!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with my code

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    You should post the full text of the error message that shows where the error happens.

    why it is getting the error
    Because the index being used has an invalid value. You should check that the variable has a valid value before using it to index a String. It looks like you are assuming the value a method returns is valid. The method can return a -1 that you need to test for BEFORE using it.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need some help with my code

    Your code is fail on MAP.charAt(Pass.indexOf(alpha.charAt(i)));

    Because there is no character in variable Pass, so the return of indexOf is -1.

    What do you want from this code ?

Similar Threads

  1. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  2. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  3. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  4. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM