4 Replies - 1091 Views - Last Post: 17 May 2009 - 03:43 PM Rate Topic: -----

#1 fefocamino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-May 09

Exception in thread "main" java.lang.StringIndexOutOfBoundsE

Posted 16 May 2009 - 12:45 PM

Hi i`m Fernando Camino, student from Chile.

i need some help in order to finish a work.

the "program" works reading a file.txt with a list of songs that are write in this way

Out Of The silent Planet -Iron Maiden -Brave New World -2000 -Metal.wma

Ghost of the Navigator -Iron Maiden -Brave New World -2000 -Metal.wma

Say Goodbye -Reel Bigfish - Were not happy till you are not happy -2005 - ska.wma

Somebody hates me -Reel Bigfish - Were not happy till you are not happy -2005 - ska.wma

brand new song -Reel Bigfish - Were not happy till you are not happy -2005 - ska.mp3


then transforming it in an "arrainge" of strings.

The problem that i have to solve is to:

1.-take all this song Strings , creating an other arrainge, but this time of songs called temas.

every temas have :

String titulo;-> tittle of the song
String interprete;->band
String album;
String año;->year
String genero;->rock, pop, punk etc...
String formato;->format mp3. wma. etc...

then i have to order it by tittle, then album, genero( rock pop punk etc.), print the band with his albums and songs, print the band with more songs, the oldest album and the newest one and print the One hits wonders( bands with one song)

the code of all 3 classes:

class convertor(transform the arrainge of Strings in to the arrainge of temas(songs):

[import smt.SongLoader;
import consola.*;

public class convertor {
temas [] listadetemas;
int validador=0;

public convertor(){}


public int CargarCanciones(){

SongLoader s1= new SongLoader("songnames.txt");
String[] songnames = s1.getSongList();
for(int i=0;i<songnames.length;i++){
songnames[i].toLowerCase();
for(int j=0;j<songnames[i].length();j++){
if(songnames[i].charAt(j)==' '&&songnames[i].charAt(j+1)==' '){
songnames[i]=songnames[i].substring(0,j)+songnames[i].substring(j);
}
if(songnames[i].charAt(j)=='-'&&songnames[i].charAt(j+1)==' '){

}
}
for(int j=0;j<songnames[i].length();j++){
songnames[i].substring(0, 0).toUpperCase();
if(songnames[i].charAt(j)=='-'){
songnames[i].substring(j+1, j+1).toUpperCase();
}
}
if(songnames[i].equals(songnames[i+1])){
Consola.MostrarMensaje(songnames[i]+"\n"+
songnames[i+1]);
}

}
listadetemas=new temas[songnames.length];

for(int i=0;i<songnames.length;i++){
listadetemas[i]=new temas(null,null,null,null,null,null);
}


for(int i=0;i<songnames.length;i++){
listadetemas[i].setTitulo(songnames[i].substring(0,songnames[i].indexOf('-')));
listadetemas[i].setAlbum(songnames[i].substring(songnames[i].indexOf('-',songnames[i].indexOf('-')+1)));
listadetemas[i].setGenero(songnames[i].substring(songnames[i].lastIndexOf('-'),songnames[i].lastIndexOf('.')));
listadetemas[i].setAño(songnames[i].substring(songnames[i].lastIndexOf('-',songnames[i].lastIndexOf('-')-1),songnames[i].lastIndexOf('-')));
listadetemas[i].setFormato(songnames[i].substring(songnames[i].lastIndexOf('.')));
listadetemas[i].setInterprete(songnames[i].substring(songnames[i].indexOf('-'),songnames[i].indexOf('-',songnames[i].indexOf('-')+1)));
}

return validador++;
}

public boolean OrdenarPorTitulo(){
if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}
boolean intercambio=false;
do{
intercambio=false;
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getTitulo().compareToIgnoreCase(listadetemas[i+1].getTitulo())<0){
temas tmp=listadetemas[i];
listadetemas[i]=listadetemas[i+1];
listadetemas[i+1]=tmp;
intercambio=true;}
}
}while (intercambio=true);
for(int i=0;i<listadetemas.length;i++){
Consola.MostrarMensaje(listadetemas[i].getTitulo()+"-"+listadetemas[i].getInterprete()+","+listadetemas[i].getAlbum()+" ("+listadetemas[i].getAño()+") ["+listadetemas[i].getGenero()+"]");

}
return true;}

public boolean OrdenarPorAlbum(){

if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}
boolean intercambio=false;
do{
intercambio=false;
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getAlbum().compareToIgnoreCase(listadetemas[i+1].getAlbum())<0){
temas tmp=listadetemas[i];
listadetemas[i]=listadetemas[i+1];
listadetemas[i+1]=tmp;
intercambio=true;}
}
}while (intercambio=true);
for(int i=0;i<listadetemas.length;i++){
Consola.MostrarMensaje(listadetemas[i].getAlbum()+"-"+listadetemas[i].getTitulo()+","+listadetemas[i].getInterprete()+", ("+listadetemas[i].getAño()+") ["+listadetemas[i].getGenero()+"]");

}
return true;
}

public boolean OrdenarPorGenero(){

if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}
boolean intercambio=false;
do{
intercambio=false;
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getGenero().compareToIgnoreCase(listadetemas[i+1].getGenero())<0){
temas tmp=listadetemas[i];
listadetemas[i]=listadetemas[i+1];
listadetemas[i+1]=tmp;
intercambio=true;}
}
}while (intercambio=true);
for(int i=0;i<listadetemas.length;i++){
Consola.MostrarMensaje(listadetemas[i].getGenero()+"-"+listadetemas[i].getTitulo()+","+listadetemas[i].getInterprete()+", ["+listadetemas[i].getAlbum()+"] ("+listadetemas[i].getAño()+")");

}
return true;
}

public boolean AgruparPorArtistaAlbum(){
if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}

String msj="";

for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getInterprete()==listadetemas[i+1].getInterprete()){

msj="-"+listadetemas[i].getInterprete()+":\n";
if(listadetemas[i].getAlbum()==listadetemas[i+1].getAlbum()){
msj="-"+listadetemas[i].getInterprete()+":" +
"\n -"+listadetemas[i].getAlbum()+" ("+listadetemas[i].getAño()+")\n";
;}
else{
msj+=" -"+listadetemas[i].getAlbum()+" ("+listadetemas[i].getAño()+")\n";
}
msj+=" +"+listadetemas[i].getTitulo();
}
msj="-"+listadetemas[i].getInterprete()+":\n" +
" -"+listadetemas[i].getAlbum()+" ("+listadetemas[i].getAño()+")\n" +
" +"+listadetemas[i].getTitulo();
}
return true;
}

public boolean ArtistasConMasCanciones(){
if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}

for(int i=0;i<listadetemas.length;i++){
int cont=0;
if(listadetemas[i].getInterprete()==listadetemas[i+1].getInterprete()){
cont++;


}




}
return true;
}

public boolean AlbumMasViejo(){
if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}
boolean intercambio=false;
do{
intercambio=false;
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getAño().compareToIgnoreCase(listadetemas[i+1].getAño())<0){
temas tmp=listadetemas[i];
listadetemas[i]=listadetemas[i+1];
listadetemas[i+1]=tmp;
intercambio=true;}
}
}while (intercambio=true);
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getAño()==listadetemas[0].getAño())
Consola.MostrarMensaje(listadetemas[i].getAño());
}

return true;
}

public boolean AlbumMasNuevo(){
if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}
boolean intercambio=false;
do{
intercambio=false;
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getAño().compareToIgnoreCase(listadetemas[i+1].getAño())<0){
temas tmp=listadetemas[i];
listadetemas[i]=listadetemas[i+1];
listadetemas[i+1]=tmp;
intercambio=true;}
}
}while (intercambio=true);
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getAño()==listadetemas[listadetemas.length-1].getAño())
Consola.MostrarMensaje(listadetemas[i].getAño());
}

return true;
}

public boolean OneHitWonders(){
if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}
for(int i=0;i<listadetemas.length;i++){
if(listadetemas[i].getInterprete()==listadetemas[i+1].getInterprete()){
if(listadetemas[i].getAlbum()==listadetemas[i+1].getAlbum()){
;}}
else Consola.MostrarMensaje("-"+listadetemas[i].getInterprete());
}
return true;
}


}]


class temas, with the atributes of all song.

[import smt.SongLoader;
import consola.*;

public class temas {

String titulo;
String interprete;
String album;
String año;
String genero;
String formato;

public temas(String t,String i,String alb,String a,String g,String f){

t=titulo;
i=interprete;
alb=album;
a=año;
g=genero;
f=formato;

}

public String getTitulo() {
return titulo;
}

public void setTitulo(String titulo) {
this.titulo = titulo;
}

public String getInterprete() {
return interprete;
}

public void setInterprete(String interprete) {
this.interprete = interprete;
}

public String getAlbum() {
return album;
}

public void setAlbum(String album) {
this.album = album;
}

public String getAño() {
return año;
}

public void setAño(String año) {
this.año = año;
}

public String getGenero() {
return genero;
}

public void setGenero(String genero) {
this.genero = genero;
}

public String getFormato() {
return formato;
}

public void setFormato(String formato) {
this.formato = formato;
}






}

class Cprincipal have the menu that ask the usser what he or she wants to do.

]
[import smt.SongLoader;
import consola.*;
import iic1102Package.Interfaz;
public class Cprincipal {

temas [] listadetemas;
int validador=0;

public static void main(String[] args) {
int n = 0;
convertor c=new convertor();
while(n != 10){

n=Interfaz.PedirEntero("1. Cargar Canciones\n" +
" 2. Ordenar por título\n" +
" 3. Ordenar por álbum\n" +
" 4. Ordenar por género\n" +
" 5. Agrupar por artista y luego por álbum\n" +
" 6. Artistas con más canciones\n" +
" 7. Album más viejo\n" +
" 8. Album más nuevo\n" +
" 9. One-hit-wonders\n" +
"10. Salir");


if(n==1){c.CargarCanciones();}
if(n==2){c.OrdenarPorTitulo();}
if(n==3){c.OrdenarPorAlbum();}
if(n==4){c.OrdenarPorGenero();}
if(n==5){c.AgruparPorArtistaAlbum();}
if(n==6){c.ArtistasConMasCanciones();}
if(n==7){c.AlbumMasViejo();}
if(n==8){c.AlbumMasNuevo();}
if(n==9){c.OneHitWonders();}
if(n==10)break;
}
}

}


]

There is some error in this java code i can't find. It would very helpfull if someone could help me to make it run.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.StringIndexOutOfBoundsE

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: Exception in thread "main" java.lang.StringIndexOutOfBoundsE

Posted 16 May 2009 - 03:29 PM

You're pretty close, but the important things are:

1. Use Comparator instances to sort a List of Tema
2. Using a Scanner to load will make things much easier. See below (note ano is treated as int)

If you give Tema a toString method, you'll see your collection printed out

import java.io.*;

import java.util.*;


public class TemaLoader {
    final String DATA = "Out Of The silent Planet -Iron Maiden -Brave New World -2000 -Metal.wma\n" +
	"Ghost of the Navigator -Iron Maiden -Brave New World -2000 -Metal.wma\n" +
	"Say Goodbye -Reel Bigfish - Were not happy till you are not happy -2005 - ska.wma\n" +
	"Somebody hates me -Reel Bigfish - Were not happy till you are not happy -2005 - ska.wma\n" +
	"brand new song -Reel Bigfish - Were not happy till you are not happy -2005 - ska.mp3";
    private List<Tema> temas;

    public void load() {
	temas = new ArrayList<Tema>();
	Scanner s = new Scanner(new StringReader(DATA));
	s.useDelimiter("[\\-\r\n]");
	while (s.hasNextLine()) {
	    Tema t = new Tema();
	    for(int i = 0;i < 5;i++) {
		t.setTitulo(s.next().trim());
		t.setInterprete(s.next().trim());
		t.setAlbum(s.next().trim());
		t.setAno(Integer.parseInt(s.next().trim()));
		t.setFormato(s.next().trim());
	    }
	    temas.add(t);
	}
	s.close();
	System.out.println(temas);
    }

    public static void main(String[] args) {
	new TemaLoader().load();
    }
}



This post has been edited by g00se: 16 May 2009 - 03:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 fefocamino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-May 09

Re: Exception in thread "main" java.lang.StringIndexOutOfBoundsE

Posted 17 May 2009 - 03:10 PM

[import smt.SongLoader;
import consola.*;
public class convertor {

temas [] listadetemas;
int validador=0;

public convertor(){}

public temas[] CargarCanciones(){

SongLoader s1= new SongLoader("songnames.txt");
String[] songnames = s1.getSongList();
for(int i=0;i<songnames.length-1;i++){
songnames[i].toLowerCase();
String msj="";
for(int j=0;j<songnames[i].length()-1;j++){

if(songnames[i].charAt(j)!='-'&&songnames[i].charAt(j)!=' '){
msj+=songnames[i].charAt(j);
}
if(songnames[i].charAt(j)==' '){
if(songnames[i].charAt(j+1)!=' '&& songnames[i].charAt(j+1)!='-'){
msj+=songnames[i].charAt(j);
}

}

if(songnames[i].charAt(j)=='-'){
if(songnames[i].charAt(j+1)!='-'&&songnames[i].charAt(j+1)!=' '){
msj+=songnames[i].charAt(j);

}
if(songnames[i].charAt(j+1)==' '){
msj+=songnames[i].charAt(j);
j++;
}
}
}
songnames[i]=msj;
for(int j=0;j<songnames[i].length()-1;j++){
songnames[i].substring(0, 0).toUpperCase();
if(songnames[i].charAt(j)=='-'){
songnames[i].substring(j+1, j+1).toUpperCase();
}
}
if(songnames[i].compareToIgnoreCase(songnames[i+1])==0){
Consola.MostrarMensaje(songnames[i]+"\n"+
songnames[i+1]);
}

}
listadetemas=new temas[songnames.length];
Consola.MostrarMensaje("carga en proceso...");
for(int i=0;i<songnames.length-1;i++){
listadetemas[i]=new temas((songnames[i].substring(0,songnames[i].indexOf('-'))),(songnames[i].substring(songnames[i].indexOf('-')+1,songnames[i].indexOf('-',songnames[i].indexOf('-')+1))),(songnames[i].substring(songnames[i].indexOf('-',songnames[i].indexOf('-')+1)+1,songnames[i].indexOf('-',songnames[i].indexOf(('-'),songnames[i].indexOf('-')+1)+1))),(songnames[i].substring(songnames[i].lastIndexOf('-',songnames[i].lastIndexOf('-')-1)+1,songnames[i].lastIndexOf('-'))),(songnames[i].substring(songnames[i].lastIndexOf('-')+1,songnames[i].lastIndexOf('.'))),(songnames[i].substring(songnames[i].lastIndexOf('.')+1)));
}


for(int i=0;i<songnames.length-1;i++){

listadetemas[i].setTitulo(songnames[i].substring(0,songnames[i].indexOf('-')));
listadetemas[i].setAlbum(songnames[i].substring(songnames[i].indexOf('-',songnames[i].indexOf('-')+1)+1,songnames[i].indexOf('-',songnames[i].indexOf(('-'),songnames[i].indexOf('-')+1)+1)));
listadetemas[i].setGenero(songnames[i].substring(songnames[i].lastIndexOf('-')+1,songnames[i].lastIndexOf('.')));
listadetemas[i].setAño(songnames[i].substring(songnames[i].lastIndexOf('-',songnames[i].lastIndexOf('-')-1)+1,songnames[i].lastIndexOf('-')));
listadetemas[i].setFormato(songnames[i].substring(songnames[i].lastIndexOf('.')+1));
listadetemas[i].setInterprete(songnames[i].substring(songnames[i].indexOf('-')+1,songnames[i].indexOf('-',songnames[i].indexOf('-')+1)));
}
Consola.MostrarMensaje(listadetemas.length+" canciones efectivamente cargadas");
validador++;
return listadetemas;
}

public boolean OrdenarPorTitulo(){
if(validador==0){Consola.MostrarMensaje("---¡debe cargar canciones!---"); return false;}

boolean intercambio=false;
do{
intercambio=false;
for(int i=0;i<listadetemas.length-1;i++){

if(listadetemas[i].getTitulo().compareTo(listadetemas[i+1].getTitulo())>0){
temas tmp=listadetemas[i];
listadetemas[i]=listadetemas[i+1];
listadetemas[i+1]=tmp;
intercambio=true;}
}
}while (intercambio=true);
for(int i=0;i<listadetemas.length-1;i++){
Consola.MostrarMensaje(listadetemas[i].getTitulo()+"-"+listadetemas[i].getInterprete()+","+listadetemas[i].getAlbum()+" ("+listadetemas[i].getAño()+") ["+listadetemas[i].getGenero()+"]");

}
return true;}]










when i run it, it sends me

Exception in thread "main" java.lang.NullPointerException
at convertor.OrdenarPorTitulo(convertor.java:82)

but i think the array is not null, (listadetemas), what could be happening??
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: Exception in thread "main" java.lang.StringIndexOutOfBoundsE

Posted 17 May 2009 - 03:41 PM

You appear to have ignored my previous post. Why?
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,901
  • Joined: 24-September 08

Re: Exception in thread "main" java.lang.StringIndexOutOfBoundsE

Posted 17 May 2009 - 03:43 PM

:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1