Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-19-2011, 03:39 AM   PM User | #1
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
Java Array Program Error :(

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 fString lint 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 0numi++)
        {
            
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(firstlastzip);
            
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.

I appreciate any help!
hypertone is offline   Reply With Quote
Old 03-19-2011, 04:39 AM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Most likely what is happening is that you are trying to make an Int from a String for something that isn't an int.

That is, your input file would look like this:

Code:
Aradon Strider 55555
Mother Strider 44455
Father Strider 21345
Your Enter Data Class reads one line, then another line, then tries to read an int. So

Code:
first == "Aradon Strider 55555"
second == "Mother Strider 44455"
zip == Father
When it comes to the "father" and putting it into an Int it doesn't work out.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 03-22-2011, 06:22 PM   PM User | #3
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
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.


I appreciate any comments/help.
hypertone is offline   Reply With Quote
Old 03-23-2011, 03:44 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Old 03-24-2011, 12:16 AM   PM User | #5
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
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!
hypertone is offline   Reply With Quote
Old 03-24-2011, 02:02 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by hypertone View Post
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
Fou-Lu is offline   Reply With Quote
Old 03-24-2011, 06:36 PM   PM User | #7
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
Awesome explanation Fou-Lu! Thanks a lot
hypertone is offline   Reply With Quote
Old 04-02-2011, 09:54 PM   PM User | #8
mimis40
New Coder

 
Join Date: Mar 2011
Location: Utah
Posts: 30
Thanks: 0
Thanked 6 Times in 6 Posts
mimis40 is on a distinguished road
try:

Code:
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);
mimis40 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:58 AM.


Advertisement
Log in to turn off these ads.