Reputation: 305
I have to read in input from a line of a text file :
1165,24
1305,27
1345,12
1360,10
1388,15
1388,20
1495,32
1680,36
and send it to a method that will build a Sales object with the first number on the line being the Product Code and the second being the Quantity Sold. Right now my code is taking the first number, creating a Sales object and setting it to both then taking the next number, creating a Sales object and setting it to both Product code and Quantity Sold.
import java.util.Scanner;
import java.io.*;
public class Proj2 {
public static void main(String[] arg) throws IOException {
String str;
Scanner fileScan, strScan;
fileScan = new Scanner (new File("SoldSorted.txt"));
System.out.println("Test");
while (fileScan.hasNext())
{
str = fileScan.nextLine ();
//System.out.println ();
strScan = new Scanner (str);
strScan.useDelimiter(",");
while (strScan.hasNext()){
//System.out.println ("Product Code: " + strScan.next());
//System.out.println ("Quantity: " + strScan.next());
Sales x = new Sales(strScan.next());
x.printSales();
}
}
}
}
public class Sales {
int productCode, quantitySold;
public Sales(String productSold) {
System.out.println("Test");
productCode = Integer.parseInt(productSold);
quantitySold = Integer.parseInt(productSold);
}
public void setProduct(int product) {
productCode = product;
}
public void setSold(int sold) {
quantitySold = sold;
}
public int getProduct() {
return productCode;
}
public int getSold() {
return quantitySold;
}
public void printSales(){
System.out.println("Product Code: " + productCode);
System.out.println("Quantity Sold: " + quantitySold);
}
}
This is my output(ignore the "Tests"):
Test
Test
Product Code: 1165
Quantity Sold: 1165
Test
Product Code: 24
Quantity Sold: 24
Test
Product Code: 1305
Quantity Sold: 1305
Test
Product Code: 27
Quantity Sold: 27
Test
Product Code: 1345
Quantity Sold: 1345
Test
Product Code: 12
Quantity Sold: 12
Test
Product Code: 1360
Quantity Sold: 1360
Test
Product Code: 10
Quantity Sold: 10
Test
Product Code: 1388
Quantity Sold: 1388
Test
Product Code: 15
Quantity Sold: 15
Test
Product Code: 1388
Quantity Sold: 1388
Test
Product Code: 20
Quantity Sold: 20
Test
Product Code: 1495
Quantity Sold: 1495
Test
Product Code: 32
Quantity Sold: 32
Test
Product Code: 1680
Quantity Sold: 1680
Test
Product Code: 36
Quantity Sold: 36
My question is how to get the numbers to associate to the right variables?
Upvotes: 0
Views: 73
Reputation: 1893
You're splitting each line into two values, then creating two new Sales objects with each of them, and each new Sales object is parsing the same value for productCode and quantitySold.
Get rid of the strScan in your main method and pass the whole line into the Sales constructor. Then in your Sales constructor, do something like this:
String[] productAndQuantity = productSold.split(",");
if (productAndQuantity.length == 2) {
productCode = Integer.parseInt(productAndQuantity[0]);
quantitySold = Integer.parseInt(productAndQuantity[1]);
} else {
// TODO handle missing values
}
Upvotes: 1