PDA

View Full Version : Beginners Java Help



Le Jingle
10-20-2012, 01:53 AM
Hello, I'm curious how I go about looping through a Scanner input txt file, line by line for integer values only?

Ex.

http://i.imgur.com/zWA01.png

I'm aware I need to loop (whether it be with or without arrays (I haven't covered array's but can't imagine them being all too different just because they're in java..) or simply integer summations for each line; How do I loop from # to #, line to line, given a Scanner object?

sumLine1 += file.nextInt(); ??

Brandon
10-20-2012, 02:33 AM
Uhh well u can try and modify this class I wrote quite some time back.. It can read files in various ways. Into an array/list. I guess that's the most useful for you. Read it into an array, split that array by spaces using the regex \\s


import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import javax.swing.ImageIcon;

public class ResourceLoader {
URI Location = null;

ResourceLoader(String FilePath) {
try {
this.Location = ResourceLoader.class.getResource(FilePath).toURI() ;
} catch (Exception Ex) {
Ex.toString();
}
}

File ToFile() {
return new File(Location);
}

byte[] ReadAsByteArray() throws FileNotFoundException {
File F = ToFile();
byte[] Result = new byte[(int)F.length()];
try {
InputStream Input = null;
try {
int TotalBytesRead = 0;
Input = new BufferedInputStream(new FileInputStream(F));
while(TotalBytesRead < Result.length) {
int BytesRemaining = Result.length - TotalBytesRead;
int BytesRead = Input.read(Result, TotalBytesRead, BytesRemaining);

if (BytesRead > 0) {
TotalBytesRead = TotalBytesRead + BytesRead;
}
}
} finally {
Input.close();
}
} catch (FileNotFoundException Ex) {
Ex.toString();
} catch (IOException Ex) {
Ex.toString();
}
return Result;
}

void WriteByteArray(byte[] Bytes) throws FileNotFoundException {
try {
OutputStream Output = null;
try {
File F = ToFile();
Output = new BufferedOutputStream(new FileOutputStream(F));
Output.write(Bytes);
} finally {
Output.close();
}
} catch (Exception Ex) {
Ex.toString();
}
}

List<String> ReadAsFileToList() throws IOException {
return Files.readAllLines(Paths.get(Location), StandardCharsets.UTF_8);
}

void WriteListToFile(List<String> Lines) throws IOException {
Files.write(Paths.get(Location), Lines, StandardCharsets.UTF_8);
}

ImageIcon ToImage() {
ImageIcon Result = null;
try {
Result = new ImageIcon(Location.toURL());
} catch (Exception Ex) {
Ex.toString();
}
return Result;
}
}

Le Jingle
10-20-2012, 06:22 AM
Ok, as tasty as Brandon's code looks, I am in my first month of Java learning (first actual class for coding too) annnd none of the above is dumbbed down for my current level of Java :/ Sorry Brandon. <3

Here's what I'm trying to do in Eclipse, I'm running into an error there, as well as my other compiler, Blue Jay:

http://i.imgur.com/2zED1.png

** And then my other compiler doesn't like the inner for loop, where I have on the line, file.nextInt();

Bobarkinator
10-21-2012, 07:11 AM
I was always a learn by example guy, so maybe this will help you.


import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Golf {

Scanner input;
int numberOfPlayers;
String[] names;
int[] scores;

public static void main(String[] args) {
Golf golf = new Golf("golfScores.txt");
golf.computeScores();
golf.printScores();
}

public Golf(String file) {
try {
input = new Scanner(new File(file));
} catch(FileNotFoundException e) {
System.out.println("Could not find file");
System.exit(0);
}
numberOfPlayers = input.nextInt();
names = new String[numberOfPlayers];
scores = new int[numberOfPlayers];
}

public void computeScores() {
for(int i = 0; i < numberOfPlayers; i++) {
names[i] = input.next(); //Stores the name of the player
scores[i] = 0;
for (int j = 0; j < 18; j++) {
scores[i] += input.nextInt();
}
}
}


//You have a few options here for printing out scores. Every java class has a "ToString" method which can be
//overridden which will let you use it in calls like System.out.println and also gives you the advantage of keeping
//your logic seperated out, which is just good practice. That's probably a little to advanced for what you're going
//for so we'll just stick the printing code in it's own method
public void printScores() {
for (int i = 0; i < numberOfPlayers; i++) {
System.out.println("The score for " + names[i] + " is " + scores[i]);
}
}
}

Le Jingle
10-21-2012, 07:25 AM
I was always a learn by example guy, so maybe this will help you.


import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Golf {

Scanner input;
int numberOfPlayers;
String[] names;
int[] scores;

public static void main(String[] args) {
Golf golf = new Golf("golfScores.txt");
golf.computeScores();
golf.printScores();
}

public Golf(String file) {
try {
input = new Scanner(new File(file));
} catch(FileNotFoundException e) {
System.out.println("Could not find file");
System.exit(0);
}
numberOfPlayers = input.nextInt();
names = new String[numberOfPlayers];
scores = new int[numberOfPlayers];
}

public void computeScores() {
for(int i = 0; i < numberOfPlayers; i++) {
names[i] = input.next(); //Stores the name of the player
scores[i] = 0;
for (int j = 0; j < 18; j++) {
scores[i] += input.nextInt();
}
}
}


//You have a few options here for printing out scores. Every java class has a "ToString" method which can be
//overridden which will let you use it in calls like System.out.println and also gives you the advantage of keeping
//your logic seperated out, which is just good practice. That's probably a little to advanced for what you're going
//for so we'll just stick the printing code in it's own method
public void printScores() {
for (int i = 0; i < numberOfPlayers; i++) {
System.out.println("The score for " + names[i] + " is " + scores[i]);
}
}
}


Hargahhhmffaaa!@ Thank you! This seems much more sensible (not only to Benland's, I mean Brandon's snippet, but the more "entry-level-learning") and looks ideal for understanding; that being said, I understand what's going on in the snippet, but I'll work with it tomorrow :)

Thanks for the help both you two!

Night,
Lj