3
import javax.swing.JOptionPane;
public class MyJavaProgramTask4Exercise3 {

    public static void main(String[] args) {

        String Namestudent, studentID;

        Namestudent = JOptionPane.showInputDialog("Type in a student name: ");
        studentID = JOptionPane.showInputDialog(null, "Type in the correspondng ID number: ");
        int the_index;

        System.out.println(Namestudent + " " +studentID);
        System.out.println(Namestudent.charAt(studentID));

    }

}

Ive been told to write a program that allows the user to type in a Student ID number and then a full name, ive done this, im stuck on this bit, to create a new string that contains the characters in the name for the index of each digit in the ID number...

i'm trying to get charAt to use the student ID the user inputs as an index reference to display the characters of Namestudent but this isnt working, what do i need to do instead thanks

Sufiyan Ghori
  • 15,959
  • 12
  • 69
  • 98
paulc01
  • 41
  • 5
  • your studentID variable is of type String, not Integer. – Sufiyan Ghori Dec 02 '14 at 16:18
  • Say the user enters the student ID number as a random number for example 3647352, I want to create a string that contains the characters of the name they enter say "john smith" and use the ID numbers as index positions of the "john smith" if you understand? say the output displayed of john smith would be hsnmh o using the number 3647352 the user entered as the ID as an idex.. – paulc01 Dec 02 '14 at 16:23
  • 1
    @paulc01 you can do it easily just make sure none of the digits will throw an indexoutofbounds exception. – brso05 Dec 02 '14 at 16:24
  • read @brso05's answer – Sufiyan Ghori Dec 02 '14 at 16:24

2 Answers2

3

Use Character.digit(char,int) to convert an ascii character digit to an int digit. We can use String.toCharArray() and that lets us use a for-each loop. Also, Java naming convention is camel-case with lower case first. Finally, I suggest defining the variables when you initialize them. Something like,

String nameStudent = JOptionPane.showInputDialog(null,
        "Type in a student name: ");
String studentId = JOptionPane.showInputDialog(null,
        "Type in the correspondng ID number: ");
for (char ch : studentId.toCharArray()) {
    int pos = nameStudent.length() % Character.digit(ch, 10);
    System.out.printf("%c @ %d = %c%n", ch, pos, nameStudent.charAt(pos));
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
  • [`Formatter` syntax](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax), `%c` is a character. `%d` *formats the argument as a decimal integer* and `%n` is newline. – Elliott Frisch Dec 02 '14 at 16:57
1
public static void main(String[] args) {

    String Namestudent, studentID;
    String newString = "";

    Namestudent = JOptionPane.showInputDialog("Type in a student name: ");
    studentID = JOptionPane.showInputDialog(null, "Type in the correspondng ID number: ");
    int the_index;
    System.out.println(Namestudent + " " + studentID);
    for(int i = 0; i < studentID.length(); i++)
    {
       newString += Namestudent.charAt(Integer.parseInt("" + studentID.charAt(i)));
       System.out.println(Namestudent.charAt(Integer.parseInt("" + studentID.charAt(i))));
    }
    System.out.println(newString);

}

Just loop through each digit of studentID and convert to Integer then get charAt of Namestudent

brso05
  • 12,634
  • 1
  • 17
  • 37
  • Thanks a lot for that it was really really helpful, where can I best find out about how all this works together what each bit means etc or can you talk me through it? for(int i = 0; i < studentID.length(); i++) { newString += Namestudent.charAt(Integer.parseInt("" + studentID.charAt(i))); System.out.println(Namestudent.charAt(Integer.parseInt("" + studentID.charAt(i)))); } System.out.println(newString); } such as, why a + is after the newString, what parseInt is and how studentID.charAT(i) works with for(int i = 0; i < studentID.length(); i++),i want to learn more – paulc01 Dec 02 '14 at 16:48
  • Basically you are looping through the entire `studentID` String and getting the char at each index "12345" charAt(i) will return 1 then 2 then 3 etc...It returns it as a char but you want to convert that char to an int so you can use that to get the charAt Namestudent. So you take Namestudent.charAt() for the int that you just parsed because charAt() accepts int not char. Then you get that char and add it to your newString. I hope this makes sense let me know if you have specific questions... – brso05 Dec 02 '14 at 16:55
  • @paulc01 also can you mark this as correct if this is what helped you? Thanks! – brso05 Dec 02 '14 at 16:56