Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to write a code which allow users to type the postcode and program return city for that postcode.

Example of text file (it is very large text file)

EHFD324 Washington
SAFRE New York
EH54 Livingston
and etc .....

I am getting error, the program only asked for "Enter the postcode". Once I entering the postcode I am getting error.

What I have tried:

C#
  public static void main(String[] args) throws FileNotFoundException
    {
        try
        {
            File f = new File("Files\\cities.txt");
            Scanner input = new Scanner(f);
            String text;
            while(input.hasNextLine())
            {
                text = input.nextLine();
                process(text);
            }


        }
        catch (FileNotFoundException ex)
        {
            System.out.println(ex.getMessage());
        }   
    }

    public static void process(String text)
    {   String name = null;
    int id;
        Scanner code = new Scanner(System.in);
        System.out.println("enter the postcode");       
        id = code.nextInt();
        Scanner data = new Scanner(text);

         if(code.equals(0))System.out.println(name);

        name = data.next();
        id = data.nextInt();

        while(data.hasNextDouble())
        {

        }
    System.out.println(name+ " ");

    }
}
Posted
Updated 21-Oct-16 6:34am
Comments
Suvendu Shekhar Giri 21-Oct-16 10:14am    
What is the error you are getting?
Reza Sami 21-Oct-16 10:23am    
The error, I am getting is:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at StringFinder.process(StringFinder.java:45)
at StringFinder.main(StringFinder.java:29)

I am not sure whether the program I wrote is correct or not?

Thanks for your reply
[no name] 21-Oct-16 10:33am    
If it were correct, you wouldn't be getting an error, would you? Learn how to use the debugger.
Reza Sami 21-Oct-16 10:36am    
Yes, you right, Do you have any idea so I can correct it
[no name] 21-Oct-16 11:56am    
Well yes, Learn to use the debugger like I already told you. It's a more valuable skill then you realize.

1 solution

Your program is written back to front. The first thing you need to do is to get the postcode from the user. And do not try to read it as an int since the codes in your file are all character strings. Once you have that field you just read the file comparing each postcode to the one entered. And lastly why are you using nextDouble when there are no double values in the text?

Look at your source file format and think carefully how you need to search through it to match the data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900