英文:
Getting out of bounds exception in java code, need help (begginer)
问题
package hr.java.vjezbe.glavna;
import java.math.BigDecimal;
import java.util.Scanner;
import hr.java.vjezbe.entitet.Artikli;
import hr.java.vjezbe.entitet.Kategorija;
import hr.java.vjezbe.entitet.Korisnik;
public class Glavna {
private static final int BROJ_KORISNIKA = 3;
private static final int BROJ_KATEGORIJA = 3;
private static int n;
public static void main(String[] args) {
Scanner skener = new Scanner(System.in);
Korisnik[] korisnici = new Korisnik[BROJ_KORISNIKA];
Kategorija[] kategorije = new Kategorija[BROJ_KATEGORIJA];
Artikli[] artikli = new Artikli[n];
for (int i = 0; i < BROJ_KORISNIKA; i++) {
System.out.println("Unesite " + (i + 1) + ". korisnika:");
System.out.print("Unesite ime >> ");
String ime = skener.nextLine();
System.out.print("Unesite prezime >> ");
String prezime = skener.nextLine();
System.out.print("Unesite E-mail >> ");
String email = skener.nextLine();
System.out.print("Unesite broj telefona >> ");
String telefon = skener.nextLine();
korisnici[i] = new Korisnik(ime, prezime, email, telefon);
}
for (int i = 0; i < BROJ_KATEGORIJA; i++) {
System.out.println("Unesite naziv " + (i + 1) + ". kategorije: ");
String naziv = skener.nextLine();
System.out.print("Unesite broj artikala za tu kategoriju >> ");
n = skener.nextInt();
skener.nextLine();
int counter = 0;
do {
System.out.print("Unesite naslov artikla >> ");
String naslov = skener.nextLine();
System.out.print("Unesite opis artikla >> ");
String opis = skener.nextLine();
System.out.print("Unesite cijenu artikla (sa zarezom) >> ");
BigDecimal cijena = skener.nextBigDecimal();
skener.nextLine();
artikli[counter] = new Artikli(naslov, opis, cijena);
counter++;
} while (counter < n);
kategorije[i] = new Kategorija(naziv, artikli);
}
skener.close();
}
}
英文:
package hr.java.vjezbe.glavna;
import java.math.BigDecimal;
import java.util.Scanner;
import hr.java.vjezbe.entitet.Artikli;
import hr.java.vjezbe.entitet.Kategorija;
import hr.java.vjezbe.entitet.Korisnik;
public class Glavna {
private static final int BROJ_KORISNIKA = 3;
private static final int BROJ_KATEGORIJA = 3;
private static int n;
public static void main(String[] args) {
Scanner skener = new Scanner(System.in);
Korisnik[] korisnici = new Korisnik[BROJ_KORISNIKA];
Kategorija[] kategorije = new Kategorija[BROJ_KATEGORIJA];
Artikli[] artikli = new Artikli[n];
for (int i = 0; i < BROJ_KORISNIKA; i++) {
System.out.println("Unesite " + (i + 1) + ". korisnika:");
System.out.print("Unesite ime >> ");
String ime = skener.nextLine();
System.out.print("Unesite prezime >> ");
String prezime = skener.nextLine();
System.out.print("Unesite E-mail >> ");
String email = skener.nextLine();
System.out.print("Unesite broj telefona >> ");
String telefon = skener.nextLine();
korisnici[i] = new Korisnik(ime, prezime, email, telefon);
}
for (int i = 0; i < BROJ_KATEGORIJA; i++) {
System.out.println("Unesite naziv " + (i + 1) + ". kategorije: ");
String naziv = skener.nextLine();
System.out.print("Unesite broj artikala za tu kategoriju >> ");
n = skener.nextInt();
skener.nextLine();
int counter = 0;
do {
System.out.print("Unesite naslov artikla >> ");
String naslov = skener.nextLine();
System.out.print("Unesite opis artikla >> ");
String opis = skener.nextLine();
System.out.print("Unesite cijenu artikla (sa zarezom) >> ");
BigDecimal cijena = skener.nextBigDecimal();
skener.nextLine();
artikli[n] = new Artikli(naslov, opis, cijena);
counter++;
} while (counter < n);
kategorije[i]= new Kategorija(naziv, artikli);
}
skener.close();
}
}
I have a problem with my code where it throws an out of bounds exception after i input the price which is called cijena. I don't know know where the problem is the code should ask the user to input the number of categories and after that make him enter it and save them in a array and after that save that array in another one called kategorije. Any help is wellcomed and advice.
答案1
得分: 0
n = skener.nextInt();
这一行代码中,你通过输入获取了一个整数值。假设是5;所以,
`n = 5;` <br><br>
在这一行代码中 =>
artikli[n] = new Artikli(naslov, opis, cijena);
你试图访问artikli[5]索引,然而你的数组大小为0。在这之前,你用n = 0来初始化了这个数组,因为未初始化的int变量默认为0=>
private static int n; // 默认为0的变量n
Artikli[] artikli = new Artikli[n]; // 创建了大小为0的名为artikli的数组
首先,你必须将数组初始化为合适的大小,然后你才能访问Artikli[n]索引。
快速解决方案:
将 -> Artikli[] artikli = new Artikli[n];
替换为 -> Artikli[] artikli = new Artikli[1000];
假设你的整数输入将小于1000,这样你就可以访问索引artikli[0到999]
<br> <br>我尽量详细地解释了。如果你有任何疑问,请留言。
英文:
n = skener.nextInt();
This line, you are taking an int value via your input. Lets say 5; so,
n = 5;
<br><br>
At this line =>
artikli[n] = new Artikli(naslov, opis, cijena);
you are trying to access artikli[5] index however the size of your array is 0. You initiated before this array with n = 0 as you didn't initialized 'n', uninitialized int variable will be 0 by default=>
private static int n; //which is 0 by dafault
Artikli[] artikli = new Artikli[n]; // creates an array named artikli of size 0
first you have to initialized the array to the appropriate size then you will be able to access your Artikli[n] index.
Quick Solution:
replace -> Artikli[] artikli = new Artikli[n];
with this-> Artikli[] artikli = new Artikli[1000];
assuming your int input will be less than 1000 so that you can access index artikli[0 to 999]
<br> <br>I've tried to explain as detailed as possible. If you have any confusions, comment.
答案2
得分: 0
你尚未声明变量 n 的值。您必须指定它,因为列表实际上不能为 0。
可以这样做:
private static int n = 3;
artikli[n] = new Artikli(naslov, opis, cijena);
错误,因为列表长度小于您要赋值的值。
如果这对您有帮助,请给我投票支持。我在这里是新手,想获得一些基本声望。谢谢! 😀
英文:
You have not declared the value of n. You have to specify it because list practically cannot be 0.
Do something like this:
private static int n = 3;
artikli[n] = new Artikli(naslov, opis, cijena);
Error because list is smaller than the values that you want give it
If this help you please vote me up. I’am here new and want to have some basic reputation. Thanks! 😀
专注分享java语言的经验与见解,让所有开发者获益!
评论