473,378 Members | 1,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. public class FindValues 
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         int capacity = 100;
  6.         int numElem = 0; 
  7.         Scanner intxt = null;
  8.         try 
  9.         {
  10.             File file = new File("data.txt");
  11.             intxt = new Scanner(file);
  12.         }
  13.         catch (FileNotFoundException ex)
  14.         {
  15.             System.out.println("File not found.");
  16.         }
  17.         int [] arr = new int [capacity];
  18.         while(intxt.hasNextInt() && numElem < capacity)
  19.         {
  20.             arr[numElem] = intxt.nextInt();
  21.             numElem++;
  22.         }
  23.         intxt.close();
  24.         Scanner in = new Scanner(System.in);
  25.         System.out.print("Enter a value (or -999 to quit): ");
  26.         int i = 0;
  27.         while(in.nextInt() != -999)
  28.         {
  29.             while(in.nextInt() != arr[i])
  30.             {
  31.                 i++;
  32.             }
  33.             if(in.nextInt() == arr[i])
  34.             {
  35.                 System.out.println(i);
  36.                 System.out.print("Enter a value (or -999 to quit): ");
  37.                 i = 0;
  38.             }
  39.             if(i == numElem)
  40.             {
  41.                 System.out.println("Not found");
  42.                 System.out.print("Enter a value (or -999 to quit): ");
  43.                 i = 0;
  44.             }
  45.         }
  46.         System.out.print("Thank you");        
  47.     }
  48. }
Apr 22 '09 #1
6 2120
JosAH
11,448 Expert 8TB
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
Apr 22 '09 #2
modified it...

Expand|Select|Wrap|Line Numbers
  1. public class FindValues 
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         int capacity = 100;
  6.         int numElem = 0; 
  7.         Scanner intxt = null;
  8.         try 
  9.         {
  10.             File file = new File("data.txt");
  11.             intxt = new Scanner(file);
  12.         }
  13.         catch (FileNotFoundException ex)
  14.         {
  15.             System.out.println("File not found.");
  16.         }
  17.         int [] arr = new int [capacity];
  18.         while(intxt.hasNextInt() && numElem < capacity)
  19.         {
  20.             arr[numElem] = intxt.nextInt();
  21.             numElem++;
  22.         }
  23.         intxt.close();
  24.         Scanner in = new Scanner(System.in);
  25.         System.out.print("Enter a value (or -999 to quit): ");
  26.         int i = 0;
  27.  
  28.         while(in.nextInt() != -999)
  29.         {
  30.             int temp = in.nextInt();
  31.             while(temp != arr[i] && i < numElem)
  32.             {
  33.                 i++;
  34.             }
  35.             if(temp == arr[i])
  36.             {
  37.                 System.out.println(i);
  38.                 System.out.print("Enter a value (or -999 to quit): ");
  39.                 i = 0;
  40.             }
  41.             if(i == numElem)
  42.             {
  43.                 System.out.println("Not found");
  44.                 System.out.print("Enter a value (or -999 to quit): ");
  45.                 i = 0;
  46.             }
  47.         }
  48.         System.out.print("Thank you");        
  49.     }
  50. }
  51.  
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...
Apr 22 '09 #3
Nepomuk
3,112 Expert 2GB
Here, have a good look at what you do in these lines:
@stupidnewb
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
Apr 22 '09 #4
Like create a variable for the first in.nextInt() in the != -999 while loop? Funky things have happened when I've tried that.
Apr 23 '09 #5
JosAH
11,448 Expert 8TB
@stupidnewb
You didn't do it correct then. Have a look at this:

Expand|Select|Wrap|Line Numbers
  1. int value;
  2. while ((value = in.nextInt()) != -999) {
  3.    // do something with 'value'
  4. }
  5.  
kind regards,

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

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: DB | last post by:
vb.net. What should I look at for acquiring scanner images? Why? WIA? TWAIN? tks, DB
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
5
by: simulacrakisma | last post by:
Hi everyone, Thanks in advance for reading this. I'm not a novice, but I'm not a PHP pro, either, nor a programming expert of any kind, so I'm kind of trying to find a solution that works for...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.