Hello! My assignment is as follows: Design and implement and application that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate format to the screen.
Now because I didn't want to go insane entering 25 sets of values, I simply set my array to 2!
First class
PHP Code:
public class PersonalData
{
private String first;
private String last;
private int zip;
public PersonalData(String f, String l, int z)
{
first = f;
last = l;
zip = z;
}
public String toString()
{
return first+"\t"+last+"/t"+zip;
}
}
Second class
PHP Code:
public class DataFile
{
private int MAX_NUM = 2;
private int num;
private PersonalData dataArray[];
public DataFile()
{
num = 0;
dataArray = new PersonalData[MAX_NUM];
}
public void addPerson(PersonalData p)
{
if (num < MAX_NUM)
{
num = num +1;
dataArray[num-1] = p;
}
}
public String toString()
{
String ret = "";
for(int i = 0; i < num; i++)
{
ret += dataArray[i].toString() + "\n";
}
return ret;
}
}
Main class
PHP Code:
import java.util.*;
public class EnterData
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
DataFile file = new DataFile();
while (scan.hasNext())
{
String first = scan.nextLine();
String last = scan.nextLine();
int zip = scan.nextInt();
PersonalData pers = new PersonalData(first, last, zip);
file.addPerson(pers);
}
System.out.println(file);
}
}
I keep getting this error
PHP Code:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at EnterData.main(EnterData.java:13)
----jGRASP wedge2: exit code for process is 1.
I've skimmed it over several times now and I can't really find anything illogical about it.
Thanks for your response Aradon. Could you recommend a better way for me to do this? I'm kind of confused as to the best way to make this work without having to redo everything.
Well this really depends on what your data actually is. If its set up as aradon has suggested, you use next() instead of nextLine() to fetch each token out, or you can fetch nextline() and use a tokenizer to split it after.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Interesting. I haven't learned about the next() command or the tokenizer yet. Guess I'm gonna start over and because I don't want to use something I haven't learned yet. Thanks for the information guys!
Interesting. I haven't learned about the next() command or the tokenizer yet. Guess I'm gonna start over and because I don't want to use something I haven't learned yet. Thanks for the information guys!
Scanner has several fetch methods within it. Next, nextline, nextint, nextdouble, etc, etc. Nextline I would typically use to pull out something complex, and then use the string tokenizer to break it down further or pattern match what I need.
Using input like so:
Code:
cat dog 100
Could bet fetched with two calls to next() followed by nextint(). Word of caution goes against using next* methods with nextline, the next based methods typically leave the line feeds on the scanner, so you should invoke the flush() before using nextline in that situation to prevent the scanner from looping.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
public PersonalData()
{
}
public String returnString(String first, String last, int zip)
{
return first+"\t"+last+"/t"+zip;
}
PersonalData pers = new PersonalData();
String info = pers.returnString(first,last,zip);
file.addPerson(info);