Problem concerning parsing int from .dat input
The following program takes information form a ".dat" file, stores it and manipulates it. However, I am having a weird problem. Whenever I try to parse a n int from a certain point in the file (the end of a line) I keep getting thrown a java.lang.NumberFormatException. I understand why this would be thrown if I was sending it a wrong number, but, I am not. In fact the token before the last one sends it the same number and it parses fine. Here is the problem code;
public void getPrice(Scanner scanner)
{
while(scanner.hasNextLine())
{
//puts into string the next scan token
String s = scanner.next();
//takes the scan toke above and puts it into an editable enviroment
stringToken = new StringTokenizer(s, " ", false);
while(stringToken.hasMoreTokens())
{
//moves position within string in file by one
position++;
/*Starts data orignazation by reading from each perspective field
* 1 = day
* 2 = day of month
* 3 = month
* 4 = year
*/
if(position == 1)
{
String dayFromFile = stringToken.nextToken();
int dayNum = Integer.parseInt(dayFromFile);
System.out.print(days[dayNum-1] +" ");
}
else if(position == 2)
{
System.out.print(stringToken.nextToken() + "/");
}
else if(position == 3)
{
System.out.print(stringToken.nextToken() + "/");
}
else if(position == 4)
{
System.out.print(stringToken.nextToken() +"\n");
}
//if it is in [buy] area, it prints
else if(position == 8)
{
String buy = stringToken.nextToken();
System.out.println("Buy: " +buy );
currentBuyPrice = Integer.parseInt(buy);
if(currentBuyPrice < 0)
currentBuyPrice = 0;
if(currentBuyPrice > buyPrice)
{
buyPrice += currentBuyPrice;
}
if(currentBuyPrice == buyPrice)
{
buyPrice +=0;
}
else
{
buyPrice -= currentBuyPrice;
}
}
//if it is in [sell] area, it prints, and resets the position to zero because line is over
else if(position == 9)
{
String sell = stringToken.nextToken();
System.out.println("Sell: " +sell);
**currentSellPrice = Integer.valueOf(sell).intValue();;
if(currentSellPrice < 0)
currentSellPrice = 0;
else if(currentSellPrice > sellPrice)
sellPrice += currentSellPrice;
else if(currentSellPrice == sellPrice)
sellPrice +=0;
else
sellPrice -= currentSellPrice;
scanner.nextLine();
position = 0;
if(scanner.hasNextLine() == false)
System.out.println("Net change of buy price: " +buyPrice +"\n Net change of sell price: " +sellPrice);
}
//discards all other string areas
else
stringToken.nextToken();
}
}
}
**The problem is here. The string prints as a perfect number, no spaces or anything. I thought it could be because the number was "-1" but I tried it without the "-" and it still threw the same thing. The really weird thing is that the buy code works fine, and it parses all ints I send it fine.
EDIT:
Here is how the .dat looks;
1 5 15 2006 18 26 12 -1 -1
1 5 15 2006 18 32 20 -1 -1
1 5 15 2006 18 38 29 -1 -1
It is the last "-1" that can not be parsed. I tried putting an excape character at the end so it looked;
1 5 15 2006 18 26 12 -1 -1 &
1 5 15 2006 18 32 20 -1 -1 &
1 5 15 2006 18 38 29 -1 -1 &
That did nothing.
[3605 byte] By [
Cybergasma] at [2007-10-2 22:05:05]

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class CSE extends JFrame implements ActionListener
{
//GUI COMPONENTS
JTextField input = new JTextField(20);
JButton submit = new JButton("submit");
//COMPONENTS FOR DATE; OBTAINING CORRECT FOLDER
String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
Calendar calSource = Calendar.getInstance();
int day = calSource.get(Calendar.DAY_OF_MONTH);
int month = calSource.get(Calendar.MONTH);
int year = calSource.get(Calendar.YEAR);
int monthCheck [] = {Calendar.JANUARY, Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL, Calendar.MAY, Calendar.JUNE, Calendar.JULY, Calendar.AUGUST, Calendar.SEPTEMBER, Calendar.OCTOBER, Calendar.NOVEMBER, Calendar.DECEMBER};
int dayS;
int monthS;
int yearS;
//if there is file found
boolean proceed = false;
//int data for analysis
int buyPrice;
int currentBuyPrice;
int sellPrice;
int currentSellPrice;
//toold for parsing and decoding input
String inputS = null;
String s = null;
StringTokenizer stringToken = null;
Scanner scanner;
int position = 0;
public CSE()
{
super("Test CSE");
setDefaultCloseOperation(EXIT_ON_CLOSE);
input.setBackground(new Color(0, 80, 250));
submit.addActionListener(this);
getContentPane().add(input, BorderLayout.SOUTH);
getContentPane().add(submit, BorderLayout.NORTH);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
getMonth();
inputS = input.getText();
FileReader newRead = null;
try {
newRead = new FileReader(monthS +"-" +day +"-" +year +"/" +inputS +".dat");
proceed = true;
}
catch(FileNotFoundException f)
{
System.out.println("File not found");
proceed = false;
}
if(proceed)
{
BufferedReader bufferedReader = new BufferedReader(newRead);
scanner = new Scanner(bufferedReader);
scanner.useDelimiter("\n");
getPrice(scanner);
}
}
public void getPrice(Scanner scanner)
{
while(scanner.hasNextLine())
{
//puts into string the next scan token
String s = scanner.next();
//takes the scan toke above and puts it into an editable enviroment
stringToken = new StringTokenizer(s, " ", false);
while(stringToken.hasMoreTokens())
{
//moves position within string in file by one
position++;
/*Starts data orignazation by reading from each perspective field
* 1 = day
* 2 = day of month
* 3 = month
* 4 = year
*/
if(position == 1)
{
String dayFromFile = stringToken.nextToken();
int dayNum = Integer.parseInt(dayFromFile);
System.out.print(days[dayNum-1] +" ");
}
else if(position == 2)
{
System.out.print(stringToken.nextToken() + "/");
}
else if(position == 3)
{
System.out.print(stringToken.nextToken() + "/");
}
else if(position == 4)
{
System.out.print(stringToken.nextToken() +"\n");
}
//if it is in [buy] area, it prints
else if(position == 8)
{
String buy = stringToken.nextToken();
System.out.println("Buy: " +buy );
currentBuyPrice = Integer.parseInt(buy);
if(currentBuyPrice < 0)
currentBuyPrice = 0;
if(currentBuyPrice > buyPrice)
{
buyPrice += currentBuyPrice;
}
if(currentBuyPrice == buyPrice)
{
buyPrice +=0;
}
else
{
buyPrice -= currentBuyPrice;
}
}
//if it is in [sell] area, it prints, and resets the position to zero because line is over
else if(position == 9)
{
String sell = stringToken.nextToken();
System.out.println("Sell: " + sell);
currentSellPrice = Integer.valueOf(sell).intValue();;
if(currentSellPrice < 0)
currentSellPrice = 0;
else if(currentSellPrice > sellPrice)
sellPrice += currentSellPrice;
else if(currentSellPrice == sellPrice)
sellPrice +=0;
else
sellPrice -= currentSellPrice;
scanner.nextLine();
position = 0;
if(scanner.hasNextLine() == false)
System.out.println("Net change of buy price: " +buyPrice +"\n Net change of sell price: " +sellPrice);
}
//discards all other string areas
else
stringToken.nextToken();
}
}
}
public void getMonth()
{
for(int x=0; x < monthCheck.length; x++)
{
if(month == monthCheck[x])
{
monthS = (x+1);
x = monthCheck.length;
}
}
}
public static void main(String [] args)
{
CSE cs = new CSE();
}
}
Make a folder named whatever the current date is, and put a .dat file in there with this;
1 5 15 2006 0 3 52 -1 -1
1 5 15 2006 0 29 52 -1 -1
1 5 15 2006 0 36 1 -1 -1
1 5 15 2006 0 42 9 -1 -1
1 5 15 2006 0 48 18 -1 -1
1 5 15 2006 0 54 29 -1 -1
1 5 15 2006 1 0 37 -1 -1
1 5 15 2006 1 6 44 -1 -1
1 5 15 2006 1 12 53 -1 -1
1 5 15 2006 1 19 1 -1 -1
1 5 15 2006 1 25 9 -1 -1
1 5 15 2006 1 31 18 -1 -1
1 5 15 2006 1 37 27 -1 -1
1 5 15 2006 1 43 37 -1 -1
1 5 15 2006 1 49 46 -1 -1
1 5 15 2006 1 55 53 -1 -1
1 5 15 2006 2 2 1 -1 -1
1 5 15 2006 2 8 10 -1 -1
1 5 15 2006 2 14 27 -1 -1
1 5 15 2006 2 20 37 -1 -1
1 5 15 2006 14 12 45 -1 -1
1 5 15 2006 14 20 36 -1 -1
1 5 15 2006 14 26 44 -1 -1
1 5 15 2006 14 32 52 -1 -1
1 5 15 2006 14 39 0 -1 -1
1 5 15 2006 14 45 8 -1 -1
1 5 15 2006 14 51 17 -1 -1
1 5 15 2006 14 57 26 -1 -1
1 5 15 2006 15 3 35 -1 -1
1 5 15 2006 15 9 43 -1 -1
1 5 15 2006 15 15 51 -1 -1
1 5 15 2006 15 21 59 -1 -1
1 5 15 2006 15 28 6 -1 -1
1 5 15 2006 15 34 15 -1 -1
1 5 15 2006 15 40 24 -1 -1
1 5 15 2006 15 46 33 -1 -1
1 5 15 2006 15 52 40 -1 -1
1 5 15 2006 15 58 48 -1 -1
1 5 15 2006 16 4 56 -1 -1
1 5 15 2006 16 11 5 -1 -1
1 5 15 2006 16 17 14 -1 -1
1 5 15 2006 16 23 24 -1 -1
1 5 15 2006 16 29 32 -1 -1
1 5 15 2006 16 35 39 -1 -1
1 5 15 2006 16 41 47 -1 -1
1 5 15 2006 16 47 55 -1 -1
1 5 15 2006 16 54 4 -1 -1
1 5 15 2006 17 0 13 -1 -1
1 5 15 2006 17 6 23 -1 -1
1 5 15 2006 17 12 31 -1 -1
1 5 15 2006 17 18 39 -1 -1
1 5 15 2006 17 24 46 -1 -1
1 5 15 2006 17 30 55 -1 -1
1 5 15 2006 17 37 3 -1 -1
1 5 15 2006 17 43 12 -1 -1
1 5 15 2006 17 49 20 -1 -1
1 5 15 2006 17 55 29 -1 -1
1 5 15 2006 18 1 36 -1 -1
1 5 15 2006 18 7 44 -1 -1
1 5 15 2006 18 13 53 -1 -1
1 5 15 2006 18 20 2 -1 -1
1 5 15 2006 18 26 12 -1 -1
1 5 15 2006 18 32 20 -1 -1
1 5 15 2006 18 38 29 -1 -1
1 5 15 2006 18 44 36 -1 -1
1 5 15 2006 18 50 45 -1 -1
1 5 15 2006 18 56 54 -1 -1
1 5 15 2006 19 3 3 -1 -1
1 5 15 2006 19 9 10 -1 -1
1 5 15 2006 19 15 18 -1 -1
1 5 15 2006 19 21 26 -1 -1
1 5 15 2006 19 27 34 -1 -1
1 5 15 2006 19 33 44 -1 -1
1 5 15 2006 19 39 53 -1 -1
1 5 15 2006 19 46 3 -1 -1
1 5 15 2006 19 52 12 -1 -1
1 5 15 2006 19 58 20 -1 -1
1 5 15 2006 20 4 30 -1 -1
1 5 15 2006 20 10 38 -1 -1
1 5 15 2006 20 16 50 -1 -1
1 5 15 2006 20 23 0 -1 -1
1 5 15 2006 20 29 9 -1 -1
1 5 15 2006 20 35 17 -1 -1
1 5 15 2006 20 41 24 -1 -1
1 5 15 2006 20 47 32 -1 -1
1 5 15 2006 20 53 40 -1 -1
1 5 15 2006 20 59 49 -1 -1
1 5 15 2006 21 5 59 -1 -1
1 5 15 2006 21 12 8 -1 -1
1 5 15 2006 21 18 16 -1 -1
1 5 15 2006 21 24 23 -1 -1
1 5 15 2006 21 30 31 -1 -1
1 5 15 2006 21 36 40 -1 -1
1 5 15 2006 21 42 49 -1 -1
1 5 15 2006 21 48 57 -1 -1
1 5 15 2006 21 55 5 -1 -1
1 5 15 2006 22 1 12 -1 -1
1 5 15 2006 22 7 20 -1 -1
1 5 15 2006 22 13 28 -1 -1
1 5 15 2006 22 19 38 -1 -1
1 5 15 2006 22 25 48 -1 -1