Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What do i have to do to fix my code?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post What do i have to do to fix my code?

    names.txt

    Basically i am just trying to search for a name within the .txt and than print line for each number after the name.
    For example: Mark 0 0 0 13 23 12 56 23 53 12 45

    i get an exception after putting in the type number: What name: Exception in thread "main" java.util.InputMismatchException

    .txt is attachted to it

    import java.io.*;
    import java.util.*;
    public class Names {
    public static void main (String [] args) throws FileNotFoundException{
    Scanner input = new Scanner(new File("names.txt"));
    Scanner console = new Scanner(System.in);
    System.out.println("Choose a task number 1-4:");
    System.out.println(" Type 1: To see histogram of a name's popularity");
    System.out.println(" Type 2: To compare two names to a specific decade");
    System.out.println(" Type 3: To show what name had a specific rank for certain decade");
    System.out.println(" Type 4: To exit");
    System.out.print("Enter type here: ");
    int type = console.nextInt();
    if (type == 1){
    System.out.print("What name: ");
    String searchName = console.nextLine(); //User types for name
    boolean found = false; // a boolean flag
    while (input.hasNextLine()) { //searches every line
    String line = input.nextLine();
    Scanner lineScan = new Scanner(line);
    int info = lineScan.nextInt();
    String name = lineScan.next(); // e.g. "Chuck"

    if (name.equalsIgnoreCase(searchName)) {
    System.out.println(name + " WAS found");
    System.out.println(info + " - ");
    found = true; // we found them!
    }
    if (!found) { // found will be true if we ever found the person
    System.out.println(searchName + " was NOT found");
    }
    }
    }


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: What do i have to do to fix my code?

    Make it not have errors. That was easy.

    On a serious note:

    • Highlight your code like so: [highlight=Java] **Code Here** [/highlight]
    • Post all of your errors stack traces, chances are few of us are going to put this into an IDE/compile it ourselves.
    • Specify the line where the error is

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: What do i have to do to fix my code?

    1. Use code tags.
    2. There are few errors in your given program.
    3. Never throw exception from main(), avoid using it. Instead catch the exception.
    4. You must look Lesson: Basic I/O

Similar Threads

  1. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2011, 12:03 AM

Tags for this Thread