# Spaceship question...

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• July 5th, 2014, 04:17 PM
javaman1
Spaceship question...
A spaceship has to travel a distance of 1500 miles away from earth. The spaceship can only travel 220 miles before it needs to refuel. It is fueled before it leaves earth and there are several fueling stations along the way. Read data from a file where the end of data is indicatded by a distance of 0 as follows:

912
510
400
0

Another set of sample data could be:
1280
720
900
1400
500
200
360
1100
0

the program should output to the user whether or not the journey is possible. For the 1st set of sample data, the journey is not possible but it is possible for the second set.

• July 5th, 2014, 04:23 PM
Cornix
Re: Spaceship question...
I would say:
2) Parse the lines of text within the file
3) Calculate whether the journey is possible or not
4) Output results to user

Now, you should think about these steps one by one and try to implement them.
• July 5th, 2014, 04:26 PM
GregBrannon
Re: Spaceship question...
What do the numbers represent? Distance of fueling stations from Earth along a straight line to the destination?
• July 5th, 2014, 04:29 PM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by GregBrannon
What do the numbers represent? Distance of fueling stations from Earth along a straight line to the destination?

yes the numbers represent fueling stations from Earth to the destination. Is there a way to read those values into an array without knowing the size of the array

--- Update ---

Quote:

Originally Posted by Cornix
I would say:
2) Parse the lines of text within the file
3) Calculate whether the journey is possible or not
4) Output results to user

Now, you should think about these steps one by one and try to implement them.

this does not help...
• July 5th, 2014, 04:36 PM
GregBrannon
Re: Spaceship question...
Quote:

Is there a way to read those values into an array without knowing the size of the array
Yes, but can you use an ArrayList?
• July 5th, 2014, 06:33 PM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by GregBrannon
Yes, but can you use an ArrayList?

I dont know what is an array list. We just did basic arrays in class so i dont think i can use that.
• July 5th, 2014, 06:36 PM
Norm
Re: Spaceship question...
You could use an array if you made it large enough that you could be sure that the data wouldn't overflow it.
• July 5th, 2014, 07:31 PM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Norm
You could use an array if you made it large enough that you could be sure that the data wouldn't overflow it.

this is what i tried but its only displaying the same number.

Code :

``` public static void main (String [] args) throws IOException{ Scanner in = new Scanner(new FileReader("distances.txt"));   int [] distance = new int [100]; int dist=in.nextInt(); while (dist!=0){   for(int i=0;i<distance.length;i++){ distance[i]=dist; }   dist=in.nextInt(); }//end while int i; for(i=0;i<distance.length;i++) System.out.printf("%d\n",distance[i]); }//end main }//end class```
• July 5th, 2014, 07:46 PM
Norm
Re: Spaceship question...
Please edit the code and format it. Logically nested statements should be indented.

Quote:

its only displaying the same number.
Please post the program's output that shows what you are talking about.
• July 6th, 2014, 10:15 AM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Norm
Please edit the code and format it. Logically nested statements should be indented.

Please post the program's output that shows what you are talking about.

Code :

```public static void main (String [] args) throws IOException{ Scanner in = new Scanner(new FileReader("distances.txt"));   int [] distance = new int [100]; int dist=in.nextInt();   while (dist!=0){ for(int i=0;i<distance.length;i++){ distance[i]=dist; }//end for   dist=in.nextInt(); }//end while   System.out.printf("%d",distance[0]); }//end main```
i want to read all the numbers from the file and be able to print them to the screen. I tried testing it by just printing the first value which from the file is 1200 however it only prints the last value in the file which is 1100.
• July 6th, 2014, 10:25 AM
Norm
Re: Spaceship question...
Please edit the code and format it. Logically nested statements should be indented.
Unformatted code is hard to read and understand.
• July 6th, 2014, 10:26 AM
Cornix
Re: Spaceship question...
Quote:

Originally Posted by javaman1
Code :

``` while (dist!=0){ for(int i=0;i<distance.length;i++){ distance[i]=dist; }//end for   dist=in.nextInt(); }//end while```

Can you explain to us what exactly you are trying to do within these lines?
By the way, it would be nice if you could correctly format your code like the java standards dictate.
• July 6th, 2014, 10:31 AM
Norm
Re: Spaceship question...
Try debugging by adding some println() statements that print out what was read from the file (dist) and what is in the array(distance). The print out will help you understand what the code is doing.
To print the contents of an array:
System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));
• July 6th, 2014, 10:38 AM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Cornix
Can you explain to us what exactly you are trying to do within these lines?
By the way, it would be nice if you could correctly format your code like the java standards dictate.

the file im reading from consists of several integers. the last value is zero. so im trying to read the values. zero is the end of file value so i must stop reading the data when a zero is encountered
• July 6th, 2014, 10:40 AM
Norm
Re: Spaceship question...
1) format the code
2) debug it by adding some printlns
• July 6th, 2014, 10:51 AM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Norm
1) format the code
2) debug it by adding some printlns

ok i added some printlsn and i made some progress. not too sure how to format the code like you said.

but the program is now reading all the data and displaying it. however after it is displayed im getting an error at the end. here is the error:

1280
720
900
1400
500
200
360
1100
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at distances.main(distances.java:12)

Process completed.
• July 6th, 2014, 10:56 AM
Norm
Re: Spaceship question...
Please post the new code with the formatting and the printlns so we can see what the code is doing.

Quote:

not too sure how to format the code like you said.
Look at other code that has been posted on the forum. Most of it is properly formatted.
Here is a sample:
Code :

``` // loop until only one left while(nbrLeft > 1) { // go around players nW times / remove player when at end int wordCnt = nW-1; // 0 to count-1 if(idx == players.length) idx = 0; // wrap around   // need this loop to skip over removed players while(wordCnt >= 0) { // move from alive element to alive element for wordCnt times if(players[idx++] == Live) { // alive ? if(wordCnt == 0) { // at the end of the words? players[idx-1] = Removed; // remove nbrLeft--; // count down players System.out.println("removed "+ (idx-1) + " " +Arrays.toString(players)); break; //>>>>>>>>>> exit word count loop } wordCnt--; // count words } if(idx == players.length) idx = 0; // wrap index around } // end while() going through words   } // end while() until 1 left```
• July 6th, 2014, 12:04 PM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Norm
Please post the new code with the formatting and the printlns so we can see what the code is doing.

Look at other code that has been posted on the forum. Most of it is properly formatted.
Here is a sample:
Code :

``` // loop until only one left while(nbrLeft > 1) { // go around players nW times / remove player when at end int wordCnt = nW-1; // 0 to count-1 if(idx == players.length) idx = 0; // wrap around   // need this loop to skip over removed players while(wordCnt >= 0) { // move from alive element to alive element for wordCnt times if(players[idx++] == Live) { // alive ? if(wordCnt == 0) { // at the end of the words? players[idx-1] = Removed; // remove nbrLeft--; // count down players System.out.println("removed "+ (idx-1) + " " +Arrays.toString(players)); break; //>>>>>>>>>> exit word count loop } wordCnt--; // count words } if(idx == players.length) idx = 0; // wrap index around } // end while() going through words   } // end while() until 1 left```

ok i made some changes and now the program is reading all the values but it is displaying each value 4 times.ow can i fix it so that it only displays each value once?
Code :

```int [] distance = new int [10];//array of size 10 int dist=in.nextInt();//read first value from file   while (dist!=0){//while value is not equal to 0 do   for(int i=0;i<distance.length;i++){//loops from 1st value to last value distance[++i]=dist;//set dist to 1st location in array   System.out.printf("%d\n",distance[i]);//display value }//end for dist=in.nextInt();//read next value }//end while   in.close(); }//end main }//end class```
• July 6th, 2014, 12:08 PM
Norm
Re: Spaceship question...
Please post the program's print out.

The last 3 lines of the code are not properly formatted.
There are many lines that are not properly aligned.
The } should be in line in the same column below the start of the line with the {
• July 6th, 2014, 12:12 PM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Norm
Please post the program's print out.

The last 3 lines of the code are not properly formatted.
There are many lines that are not properly aligned.
The } should be in line in the same column below the start of the line with the {

Code :

```int [] distance = new int [10];//array of size 10 int dist=in.nextInt();//read first value from file   while (dist!=0){//while value is not equal to 0 do   for(int i=0;i<distance.length;i++){//loops from 1st value to last value distance[++i]=dist;//set dist to 1st location in array   System.out.printf("%d\n",distance[i]);//display value }//end for dist=in.nextInt();//read next value }//end while   in.close(); }//end main }//end class 1280 1280 1280 1280 1280 720 720 720 720 720 900 900 900 900 900 1400 1400 1400 1400 1400 500 500 500 500 500 200 200 200 200 200 360 360 360 360 360 1100 1100 1100 1100 1100   Process completed.```
• July 6th, 2014, 12:19 PM
Norm
Re: Spaceship question...
The code inside of a for loop should NOT change the value of the loop's index:
Code :

```for(int i=0;i<distance.length;i++){//loops from 1st value to last value distance[++i]=dist; //<<<NO don't change i here (i++), the for does it```

What is the purpose of the for loop inside of the while loop?
Why do you want to set ALL of the values in the array to the value in dist?
• July 6th, 2014, 12:32 PM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Norm
The code inside of a for loop should NOT change the value of the loop's index:
Code :

```for(int i=0;i<distance.length;i++){//loops from 1st value to last value distance[++i]=dist; //<<<NO don't change i here (i++), the for does it```

What is the purpose of the for loop inside of the while loop?
Why do you want to set ALL of the values in the array to the value in dist?

I want to store all the values from the text file into the array.
• July 6th, 2014, 12:35 PM
Norm
Re: Spaceship question...
Quote:

I want to store all the values from the text file into the array.
Ok, what simple steps should the code take to do that? Can you make a list of the steps that can be used as a guide to write the code?
• July 6th, 2014, 12:50 PM
javaman1
Re: Spaceship question...
Quote:

Originally Posted by Norm
Ok, what simple steps should the code take to do that? Can you make a list of the steps that can be used as a guide to write the code?

i read the values into the array but its displaying each value 4 times. thats my only problem
• July 6th, 2014, 01:03 PM
Norm
Re: Spaceship question...
Quote:

thats my only problem
No, there are several problems. The first (and most important) one is you are trying to write the code before making a list of the steps the code should do.

Quote:

i read the values into the array
Did you print the full contents of the array as I suggested in post#13?

Add that statement to the code to see what is in the array.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last