killerqb 0 Newbie Poster

I'm having a problem reading in a file to a scanner, it is supposed to take in a filename and then evaluate whether it is a URL or filename, but it having trouble reading just a local file.

public boolean loadSongs(String filename) {
		
		Scanner load1 = null;
		if (filename.contains("http")) {

			try {
				URL theFile = new URL(filename);
				load1 = new Scanner(theFile.openStream());
				while (load1.hasNextLine()) {

					String tempName = load1.nextLine();
					while (tempName.length() == 0) {
						tempName = load1.nextLine();
					}
					if (tempName.contains(" //")) {
						int slash = tempName.indexOf(" //");
						tempName = tempName.substring(0, slash);
					}
					String tempArtist = load1.nextLine();
					while (tempArtist.length() == 0) {
						tempArtist = load1.nextLine();
					}
					if (tempArtist.contains(" //")) {
						int slash = tempArtist.indexOf(" //");
						tempArtist = tempArtist.substring(0, slash);
					}
					String time = load1.nextLine();
					while (time.length() == 0) {
						time = load1.nextLine();
					}
					if (time.contains("//")) {
						int slash = time.indexOf("//") - 1;
						time = time.substring(0, slash);
					}

					int colon = time.indexOf(":");
					String min = time.substring(0, colon);
					int intmin = Integer.parseInt(min);
					String sec = time.substring(colon + 1);
					int intsec = Integer.parseInt(sec);
					if (intsec >= 60) {
						int minute = intsec / 60;
						intmin = intmin + minute;
						intsec = intsec - (60 * minute);
					}
					String stringURL = load1.nextLine();

					Song song = new Song(tempName, tempArtist, intmin, intsec,
							stringURL);
					this.songList.add(song);

				}

			} catch (IOException e) {
				System.out.println("could not access URL");
				e.printStackTrace();
				return false;
			}
		} else {
			// File theFile = new File(filename);
			File theFile1 = new File(filename);
			
			try {
				
				Scanner load = new Scanner(theFile1);
				while (load.hasNextLine()) {

					String tempName = load.nextLine();
					while (tempName.length() == 0) {
						tempName = load.nextLine();
					}
					if (tempName.contains(" //")) {
						int slash = tempName.indexOf(" //");
						tempName = tempName.substring(0, slash);
					}
					String tempArtist = load.nextLine();
					while (tempArtist.length() == 0) {
						tempArtist = load.nextLine();
					}
					if (tempArtist.contains(" //")) {
						int slash = tempArtist.indexOf(" //");
						tempArtist = tempArtist.substring(0, slash);
					}
					String time = load.nextLine();
					while (time.length() == 0) {
						time = load.nextLine();
					}
					if (time.contains("//")) {
						int slash = time.indexOf("//") - 1;
						time = time.substring(0, slash);
					}

					int colon = time.indexOf(":");
					String min = time.substring(0, colon);
					int intmin = Integer.parseInt(min);
					String sec = time.substring(colon + 1);
					int intsec = Integer.parseInt(sec);
					if (intsec >= 60) {
						int minute = intsec / 60;
						intmin = intmin + minute;
						intsec = intsec - (60 * minute);
					}
					// String stringURL = load.nextLine();

					Song song = new Song(tempName, tempArtist, intmin, intsec);
					this.songList.add(song);

				}

			} catch (FileNotFoundException e) {

				e.printStackTrace();
				return false;
			}
		}
		// File theFile = new File(filename);
		// Scanner load = new Scanner(theFile);
		return true;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.