I've recently had a small class test in college for java and I need help.
I have an object called "Products" that takes (String, double, int) as it's parameters.
I've created an ArrayList
ArrayList<Product> products = new ArrayList<>();
What I'm trying to do is use a StringTokenizer to read from this format and store it into individual objects and then I want to print out the object contents.
PR0001 7.99 10
PR0002 29.99 0
PR0003 5.99 25
PR0004 99.99 50
PR0005 17.99 15
PR0006 15.99 0
PR0007 19.99 35
PR0008 39.99 40
PR0009 2.99 0
PR0010 3.99 5
My main method is:
package objects;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
public class main_object_class
{
public static void main(String[] args)
{
ArrayList<Product> products = new ArrayList<>();
String fileName = "products.txt";
try
{
Scanner file = new Scanner(new File(fileName));
while(file.hasNextLine())
{
StringTokenizer strToken = new StringTokenizer(file.next(), " ");
while(strToken.hasMoreTokens())
{
String pCode = strToken.nextToken();
System.out.println("pCode Works");
double pPrice = Double.parseDouble(strToken.nextToken());
System.out.println("pPrice Works");
int pQuantity = Integer.parseInt(strToken.nextToken());
System.out.println("pQuantity Works");
products.add(new Product(pCode,pPrice,pQuantity));
System.out.println("Storing Works");
}
}
for(int i=0;i<products.size();i++)
{
System.out.println(products);
}
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
}
My Product method:
package objects;
public class Product
{
private String pCode = "";
private double pPrice = 0;
private int pQuantity = 0;
public Product(String c, double p, int q)
{
pCode = c;
pPrice = p;
pQuantity = q;
}
public void setCode(String c)
{
pCode = c;
}
public String getCode()
{
return pCode;
}
public void setPrice(double p)
{
pPrice = p;
}
public double getPrice()
{
return pPrice;
}
public void setQuantity(int q)
{
pQuantity = q;
}
public int getQuantity()
{
return pQuantity;
}
@Override
public String toString()
{
return "Code : " + getCode() + "\tPrice : $" + getPrice() + "\tQuantity : " + getQuantity();
}
}
My Error:
run:
pCode Works
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at objects.main_object_class.main(main_object_class.java:24)
C:\Users\Alex\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
I'd really like to thank for all the input. I've tried all solutions and they all worked. Yet I'm going to go with @Jerin Joseph solution!
Read the file line by line, Scanner::nextLine()
gives you one line at a time.
StringTokenizer strToken = new StringTokenizer(file.nextLine(), " ");
Remove the while(strToken.hasMoreTokens())
, You need to check it for every token you parse.
Change it to ,
String pCode = "";
double pPrice = 0;
int pQuantity = 0;
if(strToken.hasMoreTokens()){
pCode = strToken.nextToken();
System.out.println("pCode Works");
}
if(strToken.hasMoreTokens()){
pPrice = Double.parseDouble(strToken.nextToken());
System.out.println("pPrice Works");
}
if(strToken.hasMoreTokens()){
pQuantity = Integer.parseInt(strToken.nextToken());
System.out.println("pQuantity Works");
}
products.add(new Product(pCode,pPrice,pQuantity));
System.out.println("Storing Works");
Your problem is that you check if there are Tokens left, and than call "nextToken()" 3 times. Where there cannot be a guranty that there will be 3 tokens left, unless you specifically check for it. Like so:
if(strToken.countTokens() >= 3)
{
\\ here you can call strToken.nextToken() safly for 3 times.
}
Your current approuch is a classic approuch for basic itteration. Eg
while(strToken.hasMoreElements())
{
String next = strToken.nextToken();
}
But it allows only one safe call of "nextToken()" between the bracets.
I hope this helped
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.