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 2 of 2 FirstFirst 12
Results 26 to 47 of 47

Thread: Spaceship question...

  1. #26
    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
    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));
    yea this is what i got--------------------Configuration: <Default>--------------------
    1280
    1280
    1280
    1280
    1280
    1280
    1280
    1280
    1280
    1280
    720
    720
    720
    720
    720
    720
    720
    720
    720
    720
    900
    900
    900
    900
    900
    900
    900
    900
    900
    900
    1400
    1400
    1400
    1400
    1400
    1400
    1400
    1400
    1400
    1400
    500
    500
    500
    500
    500
    500
    500
    500
    500
    500
    200
    200
    200
    200
    200
    200
    200
    200
    200
    200
    360
    360
    360
    360
    360
    360
    360
    360
    360
    360
    1100
    1100
    1100
    1100
    1100
    1100
    1100
    1100
    1100
    1100
    1100110011001100110011001100110011001100%d [1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100]

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    [1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100]
    That shows all the elements in the array have the same value (the value of the last item read).

    Now are you ready to work on the logic?
    If so work on what was suggested earlier:
    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.

  3. #28
    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
    That shows all the elements in the array have the same value (the value of the last item read).

    Now are you ready to work on the logic?
    If so work on what was suggested earlier:
    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?
    ut my code is reading data from the file. but why is it only displaying the same value?

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    why is it only displaying the same value?
    Because the logic is wrong. You need change the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    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
    Because the logic is wrong. You need change the logic.
    ok but how. I dont see where im getting the error. and i placed println all over. which line do i have to change?

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    Before writing anymore code, make a list of the steps the code needs to do to read in the values from the file and save them in an array. Once the steps are worked out, then try writing the code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    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
    Before writing anymore code, make a list of the steps the code needs to do to read in the values from the file and save them in an array. Once the steps are worked out, then try writing the code.
    1.read value from file
    2.store in array.

    i did that but its not working

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    That logic only shows one record being read from the file. The program must read more than one record.
    What are the steps needed to store a value into an array?
    For example:
    assign a value to the index
    store value into array at the location pointed to by the index

    The whole program from your logic would be:
      int i=0;                  // define an index for the array
      int dist=in.nextInt();  //read value from file
      distance[i]=dist;       //store in array

    If you want to do more than that, you need a loop and code to change the index.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    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
    That logic only shows one record being read from the file. The program must read more than one record.
    What are the steps needed to store a value into an array?
    For example:
    assign a value to the index
    store value into array at the location pointed to by the index

    The whole program from your logic would be:
      int i=0;                  // define an index for the array
      int dist=in.nextInt();  //read value from file
      distance[i]=dist;       //store in array

    If you want to do more than that, you need a loop and code to change the index.
    i did that but when i run the program im getting this:

    --------------------Configuration: <Default>--------------------
    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 test.main(test.java:9)

    Process completed.

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    i did that
    I don't know what you did without seeing the code.
    Please post the code for the program that gets that error.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    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
    I don't know what you did without seeing the code.
    Please post the code for the program that gets that error.
    ok now its just printing the last number from the file 10 times
    int [] distance = new int [10];//array of size 10
    int dist=in.nextInt();//read first value from file
    int i;
    while (dist!=0){//while value is not equal to 0 do
     
    	 for(i=0;i<distance.length;i++){//loops from 1st value to last value
    	 	   distance[i]=dist;//set dist to 1st location in array       
    	 }//end for	 	   
    dist=in.nextInt();//read next value
       }//end while
     
    int temp,count;
    for(i=dist;i <distance.length;i++){
    	for(int j=i+1;j<distance.length;j++){
     
    		if(distance[j] < distance[i]){
    			temp=distance[j];
    			distance[j]=distance[i];
    			distance[i]=temp;		
    		}
    	}
    	System.out.printf("%d",distance[i]);
    }
     
           in.close();
          }//end main
    	}//end class

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    Please edit your post and format the code.

    Also please post code that will compile and execute for testing, not just a small bit that can't be tested.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    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 your post and format the code.

    Also please post code that will compile and execute for testing, not just a small bit that can't be tested.
    import java.util.*;
    import java.io.*;
    public class Spaceship{
    	public static void main(String[]args)throws IOException{
    		Scanner in = new Scanner(new FileReader("distances.txt"));	
    int [] distance = new int [10];//array of size 10
    int dist=in.nextInt();//read first value from file
    int i;
    while (dist!=0){//while value is not equal to 0 do
     
    	 for(i=0;i<distance.length;i++){//loops from 1st value to last value
    	 	   distance[i]=dist;//set dist to 1st location in array       
    	 }//end for	 	   
    dist=in.nextInt();//read next value
       }//end while
     
    int temp,count;
    for(i=dist;i <distance.length;i++){
    	for(int j=i+1;j<distance.length;j++){
     
    		if(distance[j] < distance[i]){
    			temp=distance[j];
    			distance[j]=distance[i];
    			distance[i]=temp;		
    		}
    	}
    	System.out.printf("%d",distance[i]);
    }
     
           in.close();
          }//end main
    	}//end class
    its stll only reading and displaying the same value

  14. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    This has been discussed many times.
    The logic is wrong. You need to change the logic.
    The problem was pointed to in posts#12 and 21
    If you don't understand my answer, don't ignore it, ask a question.

  15. #40
    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
    This has been discussed many times.
    The logic is wrong. You need to change the logic.
    The problem was pointed to in posts#12 and 21
    yea i saw the posts but that does not tell me anything. How is the logic incorrect?where? how do i changed the logic?

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    What does that for loop do?
    What is the contents of the array after the for loop executes? Print its contents with this statement:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    immediately after the for loop ends.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #42
    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
    What does that for loop do?
    What is the contents of the array after the for loop executes? Print its contents with this statement:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    immediately after the for loop ends.
    this is what it printed
    --------------------Configuration: <Default>--------------------
    %d [1280, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    %d [1280, 1280, 0, 0, 0, 0, 0, 0, 0, 0]
    %d [1280, 1280, 1280, 0, 0, 0, 0, 0, 0, 0]
    %d [1280, 1280, 1280, 1280, 0, 0, 0, 0, 0, 0]
    %d [1280, 1280, 1280, 1280, 1280, 0, 0, 0, 0, 0]
    %d [1280, 1280, 1280, 1280, 1280, 1280, 0, 0, 0, 0]
    %d [1280, 1280, 1280, 1280, 1280, 1280, 1280, 0, 0, 0]
    %d [1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 0, 0]
    %d [1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 0]
    %d [1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280]
    %d [720, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280]
    %d [720, 720, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280]
    %d [720, 720, 720, 1280, 1280, 1280, 1280, 1280, 1280, 1280]
    %d [720, 720, 720, 720, 1280, 1280, 1280, 1280, 1280, 1280]
    %d [720, 720, 720, 720, 720, 1280, 1280, 1280, 1280, 1280]
    %d [720, 720, 720, 720, 720, 720, 1280, 1280, 1280, 1280]
    %d [720, 720, 720, 720, 720, 720, 720, 1280, 1280, 1280]
    %d [720, 720, 720, 720, 720, 720, 720, 720, 1280, 1280]
    %d [720, 720, 720, 720, 720, 720, 720, 720, 720, 1280]
    %d [720, 720, 720, 720, 720, 720, 720, 720, 720, 720]
    %d [900, 720, 720, 720, 720, 720, 720, 720, 720, 720]
    %d [900, 900, 720, 720, 720, 720, 720, 720, 720, 720]
    %d [900, 900, 900, 720, 720, 720, 720, 720, 720, 720]
    %d [900, 900, 900, 900, 720, 720, 720, 720, 720, 720]
    %d [900, 900, 900, 900, 900, 720, 720, 720, 720, 720]
    %d [900, 900, 900, 900, 900, 900, 720, 720, 720, 720]
    %d [900, 900, 900, 900, 900, 900, 900, 720, 720, 720]
    %d [900, 900, 900, 900, 900, 900, 900, 900, 720, 720]
    %d [900, 900, 900, 900, 900, 900, 900, 900, 900, 720]
    %d [900, 900, 900, 900, 900, 900, 900, 900, 900, 900]
    %d [1400, 900, 900, 900, 900, 900, 900, 900, 900, 900]
    %d [1400, 1400, 900, 900, 900, 900, 900, 900, 900, 900]
    %d [1400, 1400, 1400, 900, 900, 900, 900, 900, 900, 900]
    %d [1400, 1400, 1400, 1400, 900, 900, 900, 900, 900, 900]
    %d [1400, 1400, 1400, 1400, 1400, 900, 900, 900, 900, 900]
    %d [1400, 1400, 1400, 1400, 1400, 1400, 900, 900, 900, 900]
    %d [1400, 1400, 1400, 1400, 1400, 1400, 1400, 900, 900, 900]
    %d [1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 900, 900]
    %d [1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 900]
    %d [1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400]
    %d [500, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400]
    %d [500, 500, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400]
    %d [500, 500, 500, 1400, 1400, 1400, 1400, 1400, 1400, 1400]
    %d [500, 500, 500, 500, 1400, 1400, 1400, 1400, 1400, 1400]
    %d [500, 500, 500, 500, 500, 1400, 1400, 1400, 1400, 1400]
    %d [500, 500, 500, 500, 500, 500, 1400, 1400, 1400, 1400]
    %d [500, 500, 500, 500, 500, 500, 500, 1400, 1400, 1400]
    %d [500, 500, 500, 500, 500, 500, 500, 500, 1400, 1400]
    %d [500, 500, 500, 500, 500, 500, 500, 500, 500, 1400]
    %d [500, 500, 500, 500, 500, 500, 500, 500, 500, 500]
    %d [200, 500, 500, 500, 500, 500, 500, 500, 500, 500]
    %d [200, 200, 500, 500, 500, 500, 500, 500, 500, 500]
    %d [200, 200, 200, 500, 500, 500, 500, 500, 500, 500]
    %d [200, 200, 200, 200, 500, 500, 500, 500, 500, 500]
    %d [200, 200, 200, 200, 200, 500, 500, 500, 500, 500]
    %d [200, 200, 200, 200, 200, 200, 500, 500, 500, 500]
    %d [200, 200, 200, 200, 200, 200, 200, 500, 500, 500]
    %d [200, 200, 200, 200, 200, 200, 200, 200, 500, 500]
    %d [200, 200, 200, 200, 200, 200, 200, 200, 200, 500]
    %d [200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
    %d [360, 200, 200, 200, 200, 200, 200, 200, 200, 200]
    %d [360, 360, 200, 200, 200, 200, 200, 200, 200, 200]
    %d [360, 360, 360, 200, 200, 200, 200, 200, 200, 200]
    %d [360, 360, 360, 360, 200, 200, 200, 200, 200, 200]
    %d [360, 360, 360, 360, 360, 200, 200, 200, 200, 200]
    %d [360, 360, 360, 360, 360, 360, 200, 200, 200, 200]
    %d [360, 360, 360, 360, 360, 360, 360, 200, 200, 200]
    %d [360, 360, 360, 360, 360, 360, 360, 360, 200, 200]
    %d [360, 360, 360, 360, 360, 360, 360, 360, 360, 200]
    %d [360, 360, 360, 360, 360, 360, 360, 360, 360, 360]
    %d [1100, 360, 360, 360, 360, 360, 360, 360, 360, 360]
    %d [1100, 1100, 360, 360, 360, 360, 360, 360, 360, 360]
    %d [1100, 1100, 1100, 360, 360, 360, 360, 360, 360, 360]
    %d [1100, 1100, 1100, 1100, 360, 360, 360, 360, 360, 360]
    %d [1100, 1100, 1100, 1100, 1100, 360, 360, 360, 360, 360]
    %d [1100, 1100, 1100, 1100, 1100, 1100, 360, 360, 360, 360]
    %d [1100, 1100, 1100, 1100, 1100, 1100, 1100, 360, 360, 360]
    %d [1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 360, 360]
    %d [1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 360]
    %d [1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100]

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    Now what about the for loop.
    What is the for loop supposed to do? The printout shows you what it does.
    This is what the array should have in it after the first record is read and saved:
    [1280, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    and this is what it should have in it after the second record:
    [1280, 720, 0, 0, 0, 0, 0, 0, 0, 0]
    and after the third:
    [1280, 720, 900, 0, 0, 0, 0, 0, 0, 0]

    Where is the println() that printed what's in post#42? It looks like it is inside of the for loop,
    not after the for loop as suggested in post#41
    If you don't understand my answer, don't ignore it, ask a question.

  19. #44
    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
    Now what about the for loop.
    What is the for loop supposed to do? The printout shows you what it does.
    This is what the array should have in it after the first record is read and saved:
    [1280, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    and this is what it should have in it after the second record:
    [1280, 720, 0, 0, 0, 0, 0, 0, 0, 0]
    and after the third:
    [1280, 720, 900, 0, 0, 0, 0, 0, 0, 0]

    Where is the println() that printed what's in post#42? It looks like it is inside of the for loop,
    not after the for loop as suggested in post#41
    yea i mis read when u said to place it outside the loop.
    this is what it prints
    --------------------Configuration: <Default>--------------------
    an ID [1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280]
    an ID [720, 720, 720, 720, 720, 720, 720, 720, 720, 720]
    an ID [900, 900, 900, 900, 900, 900, 900, 900, 900, 900]
    an ID [1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400]
    an ID [500, 500, 500, 500, 500, 500, 500, 500, 500, 500]
    an ID [200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
    an ID [360, 360, 360, 360, 360, 360, 360, 360, 360, 360]
    an ID [1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100]

  20. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    Actually the other post's print out showed more.

    what about the for loop?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #46
    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
    Actually the other post's print out showed more.

    what about the for loop?
    i think the for loop increments to store the next value in the array. But my time is up anyway. Submitted 5 mins ago. :/

  22. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Spaceship question...

    the for loop increments to store the next value in the array
    It completely fills the array with the last value that was read.

    Here is a sample of what the logic could be:

    initialize variables
    begin loop
    read next record
    if 0 read, exit loop
    store record in array at index
    increment index
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: November 4th, 2013, 04: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