Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Awesome explanation Fou-Lu! Thanks a lot

  • #8
    New Coder
    Join Date
    Mar 2011
    Location
    Utah
    Posts
    30
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •