Results 1 to 18 of 18
  1. #1
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Angry general questions & array cannot be resolved.

    Hi :)
    just started programming in Java. My goal in this part of code was to read out the first array, while saving the number of the chart in the array to a new array.

    first question: is the code clean and the method correct?
    2nd question: i get an error when i try to print the newInt Array for no apparent reason. Any suggestions?


    Java Code:
    public class viereinsdiezweite {
    	public static void main(String[] args){
    		int[] newInt = new int[20];
    		int specialInt = 3;
    		int[] bigInt = new int[]{0,85,47,46,9,74,5,698,4,56,8,475,6,4,5,8,56,84,4584,84,56,4,656,4,64,5,6,54,1,6456,2,4556,142315,31,450,0,12321,3,2,12,5,5,3,8,5,4,867,51,3,589,4,13,587,-4,1,68,456,46,7,3,54,1,36,4,321,86,4,4,21,412,42,698,936,536,93,545,472,42,68,34,20,0,985,741,3,463,745,43,4,1,24,15,82,30,0,894,65468,4,56,484,654,68,4,5,123,213213,54,6,46,87,4,50,0,548,565,4,2,2,3,9,51,51,2,6,92,9,0,3};
    			int k = 0;
    			for(int j = 0; j < bigInt.length;j = j+1){
    				if(bigInt[j] == specialInt){
    					newInt[k] = j;
    					k++;}
    			}System.out.println(Arrays.toString(newInt));
    	}
    
    }
    thx for ur help :)

    regards

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: general questions & array cannot be resolved.

    In order to print the newInt array, you should use a for loop like this.
    Java Code:
    for(int i = 0; i < newInt.length; i++) {
        System.out.print(newInt[i] + ", ");
    }
    Also, just for future use, the names of classes should always be capitalized... It's just a Java convention.
    Just to make your code a little bit simpler, you can just say j++; instead of j = j + 1;

    Good luck!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: general questions & array cannot be resolved.

    Arrays.toString() is fine for printing arrays. But you need to explain what error you are getting. Paste it in a post.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: general questions & array cannot be resolved.

    I copied the code down and ran it myself with no errors.
    Maybe he forgot to import Arrays?

  5. #5
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: general questions & array cannot be resolved.

    Quote Originally Posted by jim829 View Post
    Arrays.toString() is fine for printing arrays. But you need to explain what error you are getting. Paste it in a post.

    Regards,
    Jim
    hey Jim, thx for the answer!

    Arrays cannot be resolved

    at viereinsdiezweite.main(viereinsdiezweite.java:21)
    is the error i get when compiling. At the same time there is an error sign right next to the Sysout.prinln- line.

    In order to print the newInt array, you should use a for loop like this.
    Java Code:
    1
    2
    3
    for(int i = 0; i < newInt.length; i++) {
    System.out.print(newInt[i] + ", ");
    }
    Also, just for future use, the names of classes should always be capitalized... It's just a Java convention.
    Just to make your code a little bit simpler, you can just say j++; instead of j = j + 1;

    Good luck!
    Hey there :) thx for ur answer aswell.
    If I'm correct the code u provided just lets me go trough the newInt. Which is not the intended use of the script.
    I want to search the bigInt for a number and then save its position on the Array to newInt.
    Sorry if I'm wrong!

    regards Nando

    //edit: sry was too fast. how do I import arrays?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: general questions & array cannot be resolved.

    Yep! That's probably it.
    Java Code:
    import java.util.Arrays;
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: general questions & array cannot be resolved.

    The way you did it worked fine. I ran your program and it worked for me. What you need to do is import java.util.Arrays; This will let you use the Arrays class.
    When I ran your code I did get the position of the numbers. So just add that import and you should be fine.

  8. #8
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: general questions & array cannot be resolved.

    Thats great news :)
    where do i have to put the java.util.Arrays; ?
    Every position i tried gave me a new error.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token "import", assert expected
    java.util.Arrays cannot be resolved to a variable
    regards

  9. #9
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: general questions & array cannot be resolved.

    You need to put it at the very top of you class. So, the very first line.
    All imports go at the top of your class.

  10. #10
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: general questions & array cannot be resolved.

    Thank you kind sirs! it worked :)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,517
    Blog Entries
    7
    Rep Power
    20

    Default Re: general questions & array cannot be resolved.

    Quote Originally Posted by pj6444 View Post
    You need to put it at the very top of you class. So, the very first line.
    All imports go at the top of your class.
    But if your class is in a package, the 'package yourpackage;' statement needs to be the first statement in your file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: general questions & array cannot be resolved.

    Has anyone of u got a tip how i can define the newInt array so that the size is flexibel (as u can see i set it to 20 cause i knew it'd be enough) ? i dont wanna have all the zero's in the output.

    regards

  13. #13
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: general questions & array cannot be resolved.

    You could use an ArrayList and just use the add() method. Or you could create a for loop that finds out how many of a specific number there are before you find the position of them.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,517
    Blog Entries
    7
    Rep Power
    20

    Default Re: general questions & array cannot be resolved.

    Quote Originally Posted by pj6444 View Post
    You could use an ArrayList and just use the add() method. Or you could create a for loop that finds out how many of a specific number there are before you find the position of them.
    The first suggestion is a good one but the second suggestion reminds me of shopping for shoes with my wife: first I have to go into all those shoe shops, just to figure out which shop I have to go to to buy a pair of shoes ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: general questions & array cannot be resolved.

    You must do it yourself. Check to see the size of the array and copy using one of the Arrays.copyOf() methods. But why do that? The ArrayList implementation of the List interface does all the bookkeeping for you so the list grows dynamically as needed. Lists are preferred over arrays as they are more flexible.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,990
    Rep Power
    5

    Default Re: general questions & array cannot be resolved.

    Quote Originally Posted by jim829 View Post
    Lists are preferred over arrays as they are more flexible.

    Regards,
    Jim
    The good thing about ArrayList is that it doesn't matter what you prefer; its both a list AND an array (internally anyway) so it satisfies everyone :) ArrayList and HashMap, what would I do without those two wonderful classes?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: general questions & array cannot be resolved.

    Thx again for ur answers! :)

    I actually took the for-loop this time, since i havent learned anything about arraylists in my classes yet.
    The forloop is part of the same exact exercise where i have to check how often a special number occurs in bigInt.
    Worked like a charm!
    Since its obv the cleaner way i hope to get to know arraylists soon.....

    regards

  18. #18
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: general questions & array cannot be resolved.

    Here's a link to the Java Doc on ArrayLists if you want to learn how to use them:
    ArrayList (Java Platform SE 7 )

Similar Threads

  1. General Java Questions
    By aaro in forum New To Java
    Replies: 9
    Last Post: 01-01-2014, 06:33 PM
  2. A few general questions
    By Dissonance in forum New To Java
    Replies: 6
    Last Post: 06-30-2012, 12:39 AM
  3. Array cannot be resolved
    By aqeel2010 in forum New To Java
    Replies: 4
    Last Post: 10-07-2011, 06:38 PM
  4. General questions about Java Garbage Collection
    By tutiplain in forum New To Java
    Replies: 5
    Last Post: 11-19-2010, 03:57 PM
  5. Replies: 9
    Last Post: 12-01-2009, 08:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •