473,937 Members | 27,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scanners and Arrays

6 New Member
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 2139
JosAH
11,448 Recognized Expert MVP
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
stupidnewb
6 New Member
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 Recognized Expert Specialist
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
stupidnewb
6 New Member
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 Recognized Expert MVP
@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
stupidnewb
6 New Member
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
2002
by: DB | last post by:
vb.net. What should I look at for acquiring scanner images? Why? WIA? TWAIN? tks, DB
19
2868
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 > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
5
29264
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); pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") + ld.GetFiles("*.master.resx"); but of course there is no + operation allowed on the FileInfo arrays returned by the GetFiles method.
3
2856
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; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
1
8724
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 Framework are said to have an element type. http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
41
5057
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 the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
6
13194
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: the two arrays {"one","two","three"},{"red","green","blue} the result one red one green
5
1524
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 a job that I'm working on. I have this client that wants to be able to control their web and retail inventory from one place (I mean, it's the same inventory).
1
2458
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 of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
16
2564
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 at just comp.lang.c++. If one of these groups is too inappropriate, just take it off from where you send your replies.) Hi.
0
9962
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8207
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6072
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4899
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4441
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3495
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.