0

Please how can you go about making a concatenated string defined

int SubNo = 1; 
String Student = "Student"; 
String Name = "Name"; 
String Matric = "Matric"; 
String Sub = "Sub"; 
String Score = "Score";
if (No > 0 && No < 2) {
    ++No;
} else if (No > 1 && No < 3) {
    ++No;
} else if (No > 2 && No < 4) {
    ++No;
} else if (No > 3 && No < 5) {
    ++No;
} else if (No > 4 && No < 6) {
    ++No;
} else if (No > 5 && No < 7) {
    ++No;
} else if (No > 6 && No < 8) {
    ++No;
} else if (No > 7 && No < 9) {
    ++No;
} else if (No > 8 && No < 10) {
    ++No;
}
switch(No) { 
    case 11: System.out.println ("Error, Maximum number of students reached"); 
             break;
}
String N = Integer.toString (No);
//1
System.out.println("Please imput your Name");
Student.concat(N).concat(Name) = sc.next();
System.out.println("\n"+"Hello "+
                   Student.concat(N).concat(Name)+","+
                   " Welcome to Student Group Score Calculator");

I intend to make a loop instance whereby a different variable in the loop defines each input, for example, if I input 1 in the first instance, 4 in the second instance and 7 in the third instance. I want it to save each of my input in these instances with student1Name, student2Name & student3Name.

I've tried using the concatenate method but it doesn't work, also if you have a better way of doing this, please share

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
JMonster18
  • 11
  • 2
  • It is not possible to declare variables with *dynamic* variable names in Java. You can't form a variable name by concatenating strings, or anything like that. The variable names can only be identifiers in the Java source code. So, basically, think of a different way to do this; e.g. use arrays and array subscripting, or use a `Map` to map "variable" names to values. – Stephen C Oct 06 '22 at 00:59
  • P.S. I reformatted your code to make it readable. If you want people to take the time to help you ... you should do that kind of thing yourself. It is (IMO) borderline disrespectful to ask someone to read something a badly formatted as the original version. In future, please take the time to format your code properly **before** you ask someone else to read it on StackOverflow. (Or else you are likely to get a pile of downvotes.) – Stephen C Oct 06 '22 at 01:03
  • @StephenC thanks for your help, sorry about the code formatting, I'm kinda new to programming, and I'm usually more inclined to do everything I find similiar on a single line. I appreciate the help though – JMonster18 Oct 07 '22 at 19:06

0 Answers0