Scanners and Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stupidnewb
    New Member
    • Apr 2009
    • 6

    Scanners and Arrays

    I have to:
    -store integers read from a file in an array
    -prompt users for values to look up in the array
    -print out the first index of the requested value
    -print not found if the value is not contained in the array
    -continue to look up values until the user enters -999

    Here is what I have:

    Code:
    public class FindValues 
    {
    	public static void main(String[] args)
    	{
    		int capacity = 100;
    		int numElem = 0; 
    		Scanner intxt = null;
    		try 
    		{
    			File file = new File("data.txt");
    			intxt = new Scanner(file);
    		}
    		catch (FileNotFoundException ex)
    		{
    			System.out.println("File not found.");
    		}
    		int [] arr = new int [capacity];
    		while(intxt.hasNextInt() && numElem < capacity)
    		{
    			arr[numElem] = intxt.nextInt();
    			numElem++;
    		}
    		intxt.close();
    		Scanner in = new Scanner(System.in);
    		System.out.print("Enter a value (or -999 to quit): ");
    		int i = 0;
    		while(in.nextInt() != -999)
    		{
    			while(in.nextInt() != arr[i])
    			{
    				i++;
    			}
    			if(in.nextInt() == arr[i])
    			{
    				System.out.println(i);
    				System.out.print("Enter a value (or -999 to quit): ");
    				i = 0;
    			}
    			if(i == numElem)
    			{
    				System.out.println("Not found");
    				System.out.print("Enter a value (or -999 to quit): ");
    				i = 0;
    			}
    		}
    		System.out.print("Thank you");		
    	}
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    And your question is? What part of your program doesn't work? Did it print an error message? If not, what happened? If so, care to show it to us?

    kind regards,

    Jos

    Comment

    • stupidnewb
      New Member
      • Apr 2009
      • 6

      #3
      modified it...

      Code:
      public class FindValues 
      {
      	public static void main(String[] args)
      	{
      		int capacity = 100;
      		int numElem = 0; 
      		Scanner intxt = null;
      		try 
      		{
      			File file = new File("data.txt");
      			intxt = new Scanner(file);
      		}
      		catch (FileNotFoundException ex)
      		{
      			System.out.println("File not found.");
      		}
      		int [] arr = new int [capacity];
      		while(intxt.hasNextInt() && numElem < capacity)
      		{
      			arr[numElem] = intxt.nextInt();
      			numElem++;
      		}
      		intxt.close();
      		Scanner in = new Scanner(System.in);
      		System.out.print("Enter a value (or -999 to quit): ");
      		int i = 0;
      		
      		while(in.nextInt() != -999)
      		{
      			int temp = in.nextInt();
      			while(temp != arr[i] && i < numElem)
      			{
      				i++;
      			}
      			if(temp == arr[i])
      			{
      				System.out.println(i);
      				System.out.print("Enter a value (or -999 to quit): ");
      				i = 0;
      			}
      			if(i == numElem)
      			{
      				System.out.println("Not found");
      				System.out.print("Enter a value (or -999 to quit): ");
      				i = 0;
      			}
      		}
      		System.out.print("Thank you");		
      	}
      }
      When the first prompt comes up to request a value it returns a blank line then if another value is typed it works as described

      I can't figure out what is causing the blank line to send...

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Here, have a good look at what you do in these lines:
        Originally posted by stupidnewb
        Code:
         // ...
        		while(in.nextInt() != -999) // You read the value here and thereby move the Scanner to the next int...
        		{
        			int temp = in.nextInt(); // ...so this is the second int you enter.
        			// ...
        		}
        //...
        See what's going on? The solution would be to save the value first and then check it, that way it will be the same one.

        Greetings,
        Nepomuk

        Comment

        • stupidnewb
          New Member
          • Apr 2009
          • 6

          #5
          Like create a variable for the first in.nextInt() in the != -999 while loop? Funky things have happened when I've tried that.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by stupidnewb
            Like create a variable for the first in.nextInt() in the != -999 while loop? Funky things have happened when I've tried that.
            You didn't do it correct then. Have a look at this:

            Code:
            int value;
            while ((value = in.nextInt()) != -999) {
               // do something with 'value'
            }
            kind regards,

            Jos

            Comment

            • stupidnewb
              New Member
              • Apr 2009
              • 6

              #7
              You're right. I forgot to change it in the rest of the loops. Brilliant, thanks.

              Comment

              Working...