Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 47

Thread: Spaceship question...

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    Can someone tell me how to go about doing this please.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Spaceship question...

    I would say:
    1) Read the file
    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.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Spaceship question...

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

  4. #4
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by GregBrannon View Post
    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 View Post
    I would say:
    1) Read the file
    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...

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Spaceship question...

    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?

  6. #6
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by GregBrannon View Post
    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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Norm View Post
    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.

    	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

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Spaceship question...

    Please edit the code and format it. Logically nested statements should be indented.

    its only displaying the same number.
    Please post the program's output that shows what you are talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Norm View Post
    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.
    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.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Spaceship question...

    Please edit the code and format it. Logically nested statements should be indented.
    Unformatted code is hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by javaman1 View Post
       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.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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));
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Cornix View Post
    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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Spaceship question...

    If you want help you should follow the suggestions that were made:
    1) format the code
    2) debug it by adding some printlns
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Norm View Post
    If you want help you should follow the suggestions that were made:
    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
    Exception in thread "main" java.util.NoSuchElementException
    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.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Spaceship question...

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

    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:
          // 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
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Norm View Post
    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:
          // 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?
    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

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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 {
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Norm View Post
    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 {
    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.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Spaceship question...

    The code inside of a for loop should NOT change the value of the loop's index:
    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?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Norm View Post
    The code inside of a for loop should NOT change the value of the loop's index:
    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.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Spaceship question...

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Spaceship question...

    Quote Originally Posted by Norm View Post
    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

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Spaceship question...

    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.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: November 4th, 2013, 05:38 AM
  2. [SOLVED] Simple Spaceship Shooting Game
    By bj12marion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2012, 08:17 AM