CodeGym
Aktion
CodeGym University
Lernen
Kurs
Aufgaben
Umfragen & Quizze
Spiele
Hilfe
Zeitplan für einen Tritt in den Hintern
Community
Benutzer
Forum
Chat
Artikel
Erfolgsstorys
Aktivität
Rezensionen
Abonnements
Helles Design
Frage
  • Rezensionen
  • Über uns
Start
Jetzt lernen
Jetzt lernen
  • Alle Fragen
hidden #10625598
Level 23
  • 29.05.2020
  • 241Aufrufe
  • 13Kommentare

ist der fehler in meiner herangehensweise? warum klappt das nicht?

Frage zur Aufgabe Eine Liste sicher abrufen
Java Syntax,  Level 10,  Lektion 11
Gelöst


Erstelle eine Liste von Ganzzahlen.
Gin 20 Ganzzahlen über die Tastatur ein.
Erstelle eine Methode, um Zahlen sicher aus der Liste abzurufen:
int elementSicherAbrufen(ArrayList<Integer> liste, int index, int standardwert)
Die Methode muss ein Listenelement basierend auf seinem Index zurückgeben.
Wenn beim Abrufen eines Elements eine Ausnahme auftritt, muss diese abgefangen werden, und die Methode muss standardwert zurückgeben.

Anforderungen:
  • Das Programm muss 20 Zahlen von der Tastatur lesen.
  • Das Programm muss Daten auf dem Bildschirm anzeigen.
  • Die Methode elementSicherAbrufen muss ein Listenelement basierend auf seinem Index zurückgeben, es sei denn, es treten Ausnahmen in der Methode auf.
  • Die Methode elementSicherAbrufen muss standardwert zurückgeben, wenn Ausnahmen in der Methode auftreten. Fange Ausnahmen ab.
  • Die Methode elementSicherAbrufen darf keine Ausnahmen auslösen.
package de.codegym.task.task10.task1017; import java.io.BufferedReader; import java.io.*; import java.util.*; /* Eine Liste sicher abrufen */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Integer> liste = new ArrayList<Integer>(); for (int i = 0; i < 20; i++) { int x = Integer.parseInt(reader.readLine()); liste.add(x); } System.out.println(elementSicherAbrufen(liste, 5, 1)); System.out.println(elementSicherAbrufen(liste, 20, 7)); System.out.println(elementSicherAbrufen(liste, -5, 9)); } public static int elementSicherAbrufen(ArrayList<Integer> liste, int index, int standardwert) { //schreib hier deinen Code int back = 0; Iterator<Integer> it = liste.iterator(); while(it.hasNext()){ if(it.next() == index){ back = it.next(); continue; } else if (!it.hasNext()){ back = standardwert; } } return back; } }
0
Kommentare (13)
  • Beliebt
  • Neu
  • Alt
Du musst angemeldet sein, um einen Kommentar schreiben zu können
Guadalupe Gagnon
Level 37 , Tampa, United States
29 Mai 2020, 18:14
Take for an example an ArrayList with just these 5 values: {10, 20, 30, 40, 50} The way that your code works is that it searches each index of the passed in ArrayList for the number 'index' from the argument, and if it does not contain this number return the default number. So if the passed in argument was 3 with a default value of 35 the code would do this: does the number at index 0 (10) = 3? no does the number at index 1 (20) = 3? no does the number at index 2 (30) = 3? no does the number at index 3 (40) = 3? no does the number at index 4 (50) = 3? no so then it would return 35 The way that the code should work is it should get the number AT the specified index of the ArrayList and if an exception is thrown then return the default number. So, once again, if the passed in argument was 3 with a default value of 35 the code should do this:: the number at index 3 is 40. Return 40. if the index passed in was 5 instead of 3 then the return value would be 35 because accessing index 5 would throw an IndexOutOfRangeException and the default value would be returned instead.
0
hidden #10625598
Level 23
29 Mai 2020, 18:39
yea right, sorry i had a chaos with my tabs, i meant to upload this one.. public static int elementSicherAbrufen(ArrayList<Integer> liste, int index, int standardwert) { //schreib hier deinen Code int back = 0; for(int i = 0; i < liste.size(); i++){ if(liste.get(i) == index){ back = liste.get(i); }else{ back = standardwert; } } return back; } my output is 1, 7, 9 -> the default values.. but why
0
Guadalupe Gagnon
Level 37 , Tampa, United States
29 Mai 2020, 19:03
elementSicherAbrufen(ArrayList<Integer> liste, int index, int standardwert) ↑ index is the index number of the list. So if the list is {10, 20, 30, 40, 50} and index = 3 {10, 20, 30, 40, 50} ↑ 40 is in index 3
0
hidden #10625598
Level 23
29 Mai 2020, 19:11
exactly :D so where is my mistake, liste.get(i) is 3 at some point and then it will match with this index 3.. but yea so im giving it the wrong value right after.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
29 Mai 2020, 19:21
i is the current loop index that is accessing each index of the list (which is very common inside for loops). This task however does not want you to do that. In my example, when i equals 3 list.get(i) would equal 40. The code you shared would compare: if(40 == 3) back = 40; This would not produce the correct results because 40 does not equal 3.
0
hidden #10625598
Level 23
29 Mai 2020, 19:25
i see, right, is it possible that we never did a excercise with this one.. so i just found on google indexOf() and it doesnt work aswell - how do i get this index number?
0
Guadalupe Gagnon
Level 37 , Tampa, United States
29 Mai 2020, 19:26
in the method it is provided as an argument. The code should use that argument. Simply put:
list.get(index)
0
hidden #10625598
Level 23
29 Mai 2020, 19:30
naah doesnt work tho if(liste.get(index) == index){ back = liste.get(i); }else{ back = standardwert; }
0
Guadalupe Gagnon
Level 37 , Tampa, United States
29 Mai 2020, 19:37
you are still comparing the wrong numbers. If the number at index 3 was 40: if(liste.get(index) == index) if(40 == 3 ) << this is what that would translate to This is incorrect and wont pass.
0
hidden #10625598
Level 23
29 Mai 2020, 19:41
i wonder how my reaction is gonna be when i finally find this out, i hope its aha okay and not ah fuck why did i forgot that
0
hidden #10625598
Level 23
29 Mai 2020, 19:45
come on tell me its 3:45 here i need to sleep
0
hidden #10625598
Level 23
29 Mai 2020, 19:45
please
0
Guadalupe Gagnon
Level 37 , Tampa, United States
29 Mai 2020, 19:47
Publicly posting solutions is against the rules here. I messaged you though.
+3
Lernen
  • Registrierung
  • Java-Kurs
  • Hilfe zu Aufgaben
  • Preise
  • Spieleprojekte
  • Java Syntax
Community
  • Benutzer
  • Artikel
  • Forum
  • Chat
  • Erfolgsstorys
  • Aktivität
  • Affiliate Program
Unternehmen
  • Über uns
  • Kontakt
  • Rezensionen
  • Medien
  • CodeGym im Bildungsbereich
  • FAQ
  • Support
CodeGym CodeGym ist ein Java-Tutorial zum Lernen von Java von Grund auf. Dieser Kurs ist perfekt dafür geeignet, Java von Beginn an zu meistern. Er enthält über 1200 Aufgaben mit Sofortüberprüfung und grundlegende Theorie zu den Java-Grundlagen. Damit auch du bei deinem Kurs erfolgreich bist, haben wir eine ganze Reihe motivierender Inhalte: Quizfragen, Programmierprojekte, Inhalte zu effizientem Lernen und zum Berufseinstieg als Java-Entwickler.
Folgen
Sprache der Oberfläche
Programmierer werden gemacht, nicht geboren © 2023 CodeGym
MastercardVisa
Programmierer werden gemacht, nicht geboren © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.