-3

So our teacher told us to create a JApplet with a highscore.

He wanted us to use an Arraylist which contains 10 integer values. If u press a JButton these values are getting displayed in a JLabel. And you can enter a number and where it is placed in the Array. Like if I enter 10 and in the other text field 0, the number 10 is the first number which gets displayed when I press the button. But the other 10 integer values are supposed to move one digit up in the array.

e.g I enter nothing I get displayed

1,2,3,4,5,6,7,8,9,10

and when I enter 10 and 0 it should display

10,1,2,3,4,5,6,7,8,9,10. 

My problem is that I don't get how to move the numbers like I can only get this thing if I enter 10 and 0:

10,2,3,4,5,6,7,8,9,10

Here is my Code:

public void neueListe (int Stelle,int Zahl, int[] highscore){
    highscore[Stelle] = Zahl;
}

public void jButton1_ActionPerformed(ActionEvent evt) {
    int Stelle = Integer.parseInt(jTextField2.getText());
    int Zahl = Integer.parseInt(jTextField1.getText());
    int[] highscore = new int [10];
    highscore[0]=1;
    highscore[1]=2;
    highscore[2]=3;
    highscore[3]=4;
    highscore[4]=5;
    highscore[5]=6;
    highscore[6]=7;
    highscore[7]=8;
    highscore[8]=9;
    highscore[9]=10;

    neueListe(Stelle,Zahl, highscore);
    jLabel1.setText(""+ highscore[0]+", " + highscore[1]+", " + highscore[2]+", "+ highscore[3] + highscore[4] + highscore[5] + highscore[6] + highscore[7] + highscore[8] + highscore[9]);
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 2
    Hint: please tell your teacher that applets are **dead end** technology. **Nobody** (who doesnt need it for an existing company product) should **waste** his time on applets these days. Use Swing or JavaFx but **stay** away from applets (and no, japplets are not better). And for your specific question: do not link to external sites. Put a [mcve] here. – GhostCat Jan 10 '17 at 19:52
  • 2
    *"didn't really know how to format the code in here"* Well, you could have known if you have taken the [tour], went through the [help] and learn [ask]. Please paste your code as code-formatted text here, not in external links, read how to make a [mcve] and as per @GhostCat comment, here's an article that supports [why not use an `Applet`](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/) – Frakcool Jan 10 '17 at 19:53
  • 1
    well japplet is in our curriculum I cant tell them what to do if we get this in our A levels.. – Leon Pielenz Jan 10 '17 at 19:57
  • 2
    No, don't worry about applet/japplet for now, but please do improve your question. Please check the links provided by the two folks above, so that this question becomes answerable. – Hovercraft Full Of Eels Jan 10 '17 at 19:58
  • You need to post well-formatted relevant code, preferably a [mcve], here with your question and not in a link. – Hovercraft Full Of Eels Jan 10 '17 at 20:02
  • I edited the code now into my question :) Now I really need an answer :'D @GhostCat – Leon Pielenz Jan 10 '17 at 20:05
  • 1. You are using an array, not an ArrayList. 2. Basically, your question is how can I insert number x at position y in an ArrayList. The answer to this question is here: http://stackoverflow.com/questions/7847635/insert-at-any-position-in-java-util-list – NickL Jan 10 '17 at 20:18
  • 1
    Possible duplicate of [Insert at any position in java.util.List](http://stackoverflow.com/questions/7847635/insert-at-any-position-in-java-util-list) – NickL Jan 10 '17 at 20:19
  • Welcome to Stack Overflow! It looks like you are asking for homework help. While we have no issues with that per se, please observe these [dos and don'ts](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845), and edit your question accordingly. – Joe C Jan 10 '17 at 22:07

1 Answers1

0

Convert your int[] into ArrayList and then simply add any element at any position using add method.

ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(highscore));

arr.add(Zahl, Stelle);  // arr.add(position, value) 
System.out.println(arr);

if you want to print all no.s as string then use this.

String labelshow = "";
for(Integer item: arr){
   labelshow += "," + item;
}
jLabel1.setText(labelshow);

Or you can simply put your no. in required position and shift rest of the elements towards right using a for loop.(size would be increased keep this in mind.)

 int newarray[] = new int[highscore.length+1];
 for(int i=0, j=0; i<highscore.length+1; i++){
     if(i == Zahl){
          newarray[i] = Stelle;
     }
     else{
          newarray[i] = highscore[j++];
     }
 }

newarray contains your resultant array. You can print it or show it in JLabel.

jack jay
  • 2,493
  • 1
  • 14
  • 27
  • we have to use `int[]` ._. – Leon Pielenz Jan 10 '17 at 20:31
  • @LeonPielenz Then I would recommend to write your own little wrapper around an int array - that allows you to insert data at an index, and that then moves data around. – GhostCat Jan 10 '17 at 20:34
  • @GhostCat sry but I dont know what you mean with "wrapper" :c – Leon Pielenz Jan 10 '17 at 20:39
  • i have provided you with the `for loop` variant solution of your problem. let me know if still you dont get it right. – jack jay Jan 10 '17 at 20:40
  • @jackjay do u mean i – Leon Pielenz Jan 10 '17 at 20:49
  • @jackjay Okay, give me some minutes to understand it :'D – Leon Pielenz Jan 10 '17 at 20:51
  • its like you are traversing the `highscore` array one by one and copying into `newarray` once you found the position where you have to enter your no. you put your no. into `newarray` then again copy rest of the `highscore` array. – jack jay Jan 10 '17 at 20:53
  • @jackjay wow i understand it :D im actually smiling irl :) with this method I can only add one number to the list, right? Becouse if I add another one here: jLabel1.setText(""+ NeueListe[0]+", " + NeueListe[1]+", " + NeueListe[2]+", " + NeueListe[3]+", " + NeueListe[4]+", " + NeueListe[5]+", " + NeueListe[6]+", " + NeueListe[7]+", " + NeueListe[8]+", " + NeueListe[9] + ", " + NeueListe[10]); it wouldn't work becouse it cant display it at the first try becouse there is no number at place 11. And yes i renamed newarray into NeueListe :D – Leon Pielenz Jan 10 '17 at 21:03
  • correct, it can only add one no. at a time but you can put this inside a function and call it again and agian while the function returns you `newarray`. But i suggest you to use my first method of ArrayList if you are trying to make more than one element additions in `highscore` – jack jay Jan 10 '17 at 21:07