473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ArrayIndexOutOfBoundsException

oll3i
679 512MB
Hi!
i just started learning java ;]
i wanna write bank accounts program
i get an error i know its in login method but i dont know where :\
cd u please help me with this one

the code below reads user data from a file then sets the vars declared in a class

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at konta_bankowe.Uzytkownik.login(Uzytkownik.java)
at konta_bankowe.Konto.main(Konto.java)

my code for user
Expand|Select|Wrap|Line Numbers
  1. package konta_bankowe;
  2.  
  3.  
  4. import java.io.*;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Set;
  8. import java.io.IOException;
  9. import java.util.*;
  10. import javax.swing.*;
  11.  
  12. public class Uzytkownik {
  13.     private String  IDuzytkownika;
  14.     private String haslo;
  15.     private String numerKonta;
  16.     private String imie;
  17.     private String nazwisko;
  18.  
  19.  
  20.      public Uzytkownik(){};
  21.  
  22.     public void setUserID(String id){
  23.         IDuzytkownika=id;
  24.     }
  25.  
  26.     public void setHaslo(String h){
  27.         haslo=h;
  28.     }
  29.  
  30.     private void setNumerKonta(String n_k){
  31.     numerKonta    =n_k;
  32.     }
  33.  
  34.     private void setImie(String i){
  35.         imie=i;
  36.         }
  37.     private void setNazwisko(String n){
  38.         nazwisko=n;
  39.         }
  40.  
  41.     public void displayUserData(){
  42. System.out.println("Numer konta"+numerKonta+" "+imie+" "+nazwisko);
  43.         }
  44.  
  45. public boolean login(){
  46.      Map<String,String> map = new HashMap<String,String>();
  47.  
  48.         try{
  49.             // Open the file that is the first 
  50.             // command line parameter
  51.             FileInputStream fstream = new FileInputStream(IDuzytkownika+".txt");
  52.             // Get the object of DataInputStream
  53.             DataInputStream in = new DataInputStream(fstream);
  54.                 BufferedReader br = new BufferedReader(new InputStreamReader(in));
  55.             String strLine;
  56.  
  57.  
  58.  
  59.           while((strLine = br.readLine()) != null)   {   
  60.           String[] dane=strLine.split(":"); 
  61.           map.put(dane[0],dane[1]);
  62.           }
  63.             in.close();
  64.           }catch (FileNotFoundException e) {
  65.                 System.err.println("Nie ma takiego uzytkownika(File not found!)");
  66.           }catch (IOException e) {
  67.                   System.err.println(e);}
  68.  
  69.  
  70.  
  71. if((map.get("id")==IDuzytkownika) && (map.get("haslo")==haslo)){
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.                String numer_konta=map.get("numer konta");
  79.                String i=map.get("imie");
  80.                String n=map.get("nazwisko");
  81.  
  82.  
  83.                setNumerKonta(numer_konta);
  84.                setImie(i);
  85.                setNazwisko(n);
  86.               return  true;
  87.             }else{return false;}
  88. }
  89. }
Mar 10 '07 #1
2 1609
horace1
1,510 Expert 1GB
you don't appear to have included your main()
Mar 11 '07 #2
r035198x
13,262 8TB
Hi!
i just started learning java ;]
i wanna write bank accounts program
i get an error i know its in login method but i dont know where :\
cd u please help me with this one

the code below reads user data from a file then sets the vars declared in a class

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at konta_bankowe.Uzytkownik.login(Uzytkownik.java)
at konta_bankowe.Konto.main(Konto.java)

my code for user
Expand|Select|Wrap|Line Numbers
  1. package konta_bankowe;
  2.  
  3.  
  4. import java.io.*;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Set;
  8. import java.io.IOException;
  9. import java.util.*;
  10. import javax.swing.*;
  11.  
  12. public class Uzytkownik {
  13.     private String IDuzytkownika;
  14.     private String haslo;
  15.     private String numerKonta;
  16.     private String imie;
  17.     private String nazwisko;
  18.  
  19.  
  20.      public Uzytkownik(){};
  21.  
  22.     public void setUserID(String id){
  23.         IDuzytkownika=id;
  24.     }
  25.  
  26.     public void setHaslo(String h){
  27.         haslo=h;
  28.     }
  29.  
  30.     private void setNumerKonta(String n_k){
  31.     numerKonta    =n_k;
  32.     }
  33.  
  34.     private void setImie(String i){
  35.         imie=i;
  36.         }
  37.     private void setNazwisko(String n){
  38.         nazwisko=n;
  39.         }
  40.  
  41.     public void displayUserData(){
  42. System.out.println("Numer konta"+numerKonta+" "+imie+" "+nazwisko);
  43.         }
  44.  
  45. public boolean login(){
  46. Map<String,String> map = new HashMap<String,String>();
  47.  
  48.         try{
  49.          // Open the file that is the first 
  50.          // command line parameter
  51.          FileInputStream fstream = new FileInputStream(IDuzytkownika+".txt");
  52.          // Get the object of DataInputStream
  53.          DataInputStream in = new DataInputStream(fstream);
  54.          BufferedReader br = new BufferedReader(new InputStreamReader(in));
  55.          String strLine;
  56.  
  57.  
  58.  
  59.          while((strLine = br.readLine()) != null) { 
  60.          String[] dane=strLine.split(":"); 
  61.          map.put(dane[0],dane[1]);
  62.          }
  63.          in.close();
  64.          }catch (FileNotFoundException e) {
  65.              System.err.println("Nie ma takiego uzytkownika(File not found!)");
  66.          }catch (IOException e) {
  67.          System.err.println(e);}
  68.  
  69.  
  70.  
  71. if((map.get("id")==IDuzytkownika) && (map.get("haslo")==haslo)){
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.              String numer_konta=map.get("numer konta");
  79.              String i=map.get("imie");
  80.              String n=map.get("nazwisko");
  81.  
  82.  
  83.              setNumerKonta(numer_konta);
  84.              setImie(i);
  85.              setNazwisko(n);
  86.              return true;
  87.             }else{return false;}
  88. }
  89. }
Here is the culprit code

Expand|Select|Wrap|Line Numbers
  1. String[] dane=strLine.split(":"); 
  2.  
Creates an array of strings whose size depends on the number of ":" in the String strLine. In this case there are no ":" at all in that String (you can verify that by printing the strLine to console and see its value). So the array created has only one element at postion 0 and its length is therefore 1. When you then do

Expand|Select|Wrap|Line Numbers
  1.  map.put(dane[0],dane[1]);
  2.  
You try to access positon 1 of the array which is not in the array and thus you get the exception.
Mar 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
2
by: David Stevenson | last post by:
Programs from: Sheng Liang, The Java Native Interface, Programmer's Guide and Specification, The Java Series, (c) 1999, pp. 38-39. I thought I copied the program pretty exactly, but I don't know...
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
31
by: ivannavi | last post by:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String Temp = jComboBox1.getSelectedItem().toString(); ...
5
by: cazconv2007 | last post by:
hi i need to catch and try exceptions , one for int and one for an array out of bounds i dont undertand how my code is'nt working for array out of bounds heres my code import java.util.* ; ...
22
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to...
2
by: Richard Maher | last post by:
Hi, I would dearly love a server with a "self-signed certificate" to be accessible by my javax.net.ssl.startHandshake() but it seems to consistently crap-out with a failure :-( Look I've only...
0
RedSon
by: RedSon | last post by:
Chapter 2: How to handle Exceptions When you call a program, sometimes you get a message about an Unhandled exception type. This means, that something throws (or might throw) an Exception, but the...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
6
by: Light | last post by:
Hi I m getting this error while i m trying to run my program and i don't understand why that happens. import java.util.*; public class Exercise6 {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.