1

so this is my first real java program i have to write. I'm also fairly new to java..

The program must run with 2 command-line arguments, which are suppose to be x and y coordinates and then determines in which municipality and county the coordinates are. For this I'm suppose to use a 'winding number'.

BUT before I can start with those parts of the program, I first need to create and fill different array's , right?

The .txt file contains the following: (All places and coords are in the Netherlands btw) ID number county municipality. for example:

0 Groningen Haren

1 Groningen Leek

etc etc for the first 518 lines. The next 61650 lines or so are coordinates looking like this:

0 6.665039 53.181004

0 6.666431 53.180642

Where the first integer number ( here 0) corresponds with the municipality

To get back to the question. The textfile can not be altered, I have to use redirection(?) like in the cmd: java 'programname' < filename.txt. and it should read the entire file, create 1 array with only id numbers which are all integers. An array with the county and an array with municipality, right? But HOW do I create 3 different arrays ( oke 2, 1 integer array and 2 string arrays) when the data is on the same line..

I'm suppose to use StdIn.readInt, StdIn.readString and StdIn.readLine but I just cannot make it work.

Thanks in advance for any help.

This is what I have so far, It gives error because ( I think it's because of this) it expects to read an integer but there is also strings on that same line..

public class test{

 public static void main(String[] args){


   int n = 519;
   Integer[] a = new Integer[n];
   for(int i = 0; i < n; i++){
   a[i] = StdIn.readInt();
   StdOut.println(a[i]);
    }
 }
}
8
  • 3
    Welcome to Stackoverflow Binair! Please show your efforts so that we can give you specific help. Please read stackoverflow.com/help/how-to-ask for more information on how to ask a good question. Commented Oct 23, 2015 at 13:55
  • Okay, please but this information inside the question (edit it for this) and use the code formatter so that readers can nicely read it. Commented Oct 23, 2015 at 14:04
  • Sorry that looks like a pile of horse dung, Im not yet used to the site, will work on this! Commented Oct 23, 2015 at 14:08
  • I don't think, that anyone here will do your homework for you Commented Oct 23, 2015 at 14:10
  • 2
    They dont have to do with for me, I'm not asking people to do it for me. I'm stuck at a critic point of my assignment. I'm just providing insight on the whole assignment and am asking HOW to create these arrays from a .txt file. Asking for help is what stack is all about, right? Commented Oct 23, 2015 at 14:16

1 Answer 1

1

Since different entries are separated by line, read a complete line, then split it at the space character and then convert the first to an int and assign it to the int array and the other two to your string arrays:

String line = StdIn.readLine();
String[] split = line.split(" ");
intArray[X] = Integer.parseInt(split[0]);
stringArray1[X] = split[1];
stringArray2[X] = split[2];

You can do this in a loop.

To read a file like java -jar your.jar < inputFile.txt you need to set the input stream of your reader to System.in, on how to do it (or if StdIn does that for you) I cannot tell, without knowing the source for StdIn.


EDIT

When I understood you correctly, then you don't need to do anything, StdIn initializes itself automatically with System.in.

This means StdIn.readLine() will return the first line of the input file. You can read each line of the file with:

while (StdIn.hasNextLine())
{
    String line = StdIn.readLine();
    // stuff from above
}

To get x and y from the command line argument you need to access the String[] args argument from the main method:

int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);

Note that all code I wrote here does not check for any exceptions that might occure (for example, if you don't enter two ints). I guess this depends on your task (if it says you can assume correct input, the above code should be fine).

You can then execute your java program like this: java -jar your.jar 100 20 < inputFile.txt. Where 100 is the value for x and 20 the value for y and inputFile.txt the file with the location data.

Sign up to request clarification or add additional context in comments.

3 Comments

The StdIn is a library from java if that is what you mean by source? Forgive me, I'm such a newb..
StdIn is not a 'native' java library (native in that it is not in the java runtime environment). I did a little search and it seems the Princeton University provides this class in their standard java library. So I guess that you got this library from your university/school/etc.... So without information from where you got this class, I can't help you.
That is correct, My professor made us download these libraries, StdIn, StdOut and StdDraw. I openend it in the compiler we're suppose to use.. and well frankly it doesn't make a lot of sense to me, I can send you the libraries or provide a download link? If it helps.. No idea whats allowed here, since this is really my first ever experience on Stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.