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.

Results 1 to 25 of 25

Thread: Not sure where my problem is

  1. #1
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Not sure where my problem is

    Hello all, I have an assignment for my programming class. Quite frankly, I wouldn't be taking it if it wasn't required for my degree.

    Up to this point, I took 2 classes of C++. And I was told that Java and C++ were so very similar that I wouldn't have much trouble transitioning.

    Well, I'm getting an error message, and I'm not sure where the problem is.

    this is my itty bitty program such as it is:

    public class Main {
     
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
     
            System.out.println("Please input a 5 digit number");
     
    	int input = in.nextInt();
            int num1=0, num2=0, num3=0, num4=0, num5=0;
     
            while (input > 0)
            {
                System.out.println(input % 10);
                input = input /10;
            }
        }
    }


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

    Default Re: Not sure where my problem is

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.
    Well, I'm getting an error message, and I'm not sure where the problem is.
    Posting the error you're getting, helps us help you rather than us guessing what you're seeing. If it's a run time error, copy the console contents and post that.

  3. #3
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    I was using the code brackets on a C++ forum when I was taking those classes. I took a shot in the dark to see whether or not those brackets would work here too. Glad I was right.

    I'm using NetBeans 8.0.

    Here is the error message:
    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: class Scanner
    location: class chapter.pkg2.exercise.pkg2.pkg30.Chapter2Exercise2 30
    at chapter.pkg2.exercise.pkg2.pkg30.Chapter2Exercise2 30.main(Chapter2Exercise230.java:7)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

    Mind you, I'm not seeing anything about inputting any numbers (which is the whole goal of this project)

  4. #4
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Not sure where my problem is

    Quote Originally Posted by mattig89ch View Post
    I'm using NetBeans 8.0.

    Here is the error message:

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: class Scanner
    location: class chapter.pkg2.exercise.pkg2.pkg30.Chapter2Exercise2 30
    at chapter.pkg2.exercise.pkg2.pkg30.Chapter2Exercise2 30.main(Chapter2Exercise230.java:7)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)
    That is the opposite of a "successful" build. The compiler cannot turn your code into a real class you can instantiate.

    NetBeans should show you exactly what is wrong. Hint: if you don't know what Scanner is, how does the compiler? Scanner belongs to a named package, not the default package, and you don't define it in this code. Where does it come from? What would you do in a C++ class that needed another class?

  5. #5
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    I would reference the location of that other class at the top of the file with an include statement. Do I have to include something? I didn't see anything includes in the text book.

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

    Default Re: Not sure where my problem is

    In C/C++ the include is used as a pre-processor directive to identify other libraries needed to compile the source code. In java, the import statement is used to identify other sources of code needed to compile the program. In this case, as already mentioned, the compiler doesn't know what Scanner is, and Netbeans should be indicating that with red squigglies or some other indicator that the Scanner class is unknown. If you hover your mouse over the indicator, there may even be fixes offered to correct the problem, but I'm not 100% sure how Netbeans works.

  7. #7
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    I took another look at the text book. Yea, I needed to use an import statment. that line was buried. took me 20 min to find it.
    For those who are wondering, you need to use import java.util.Scanner;.

    right now, i'm trying to put some bells and whistles into my program. Trying to reverse the order of the numbers being displayed. Right now they are last to first, I want them to be first to last.

    My idea to do this was to use an array, and have the numbers write to the array. Then have the array output in reverse order. Trouble is, trying to declare the array is giving me an error.

    Heres the error message I'm getting:

    ant -f "C:\\Users\\Matt\\Documents\\NetBeansProjects\\Cha pter 2 Exercise 2.30" -Dnb.internal.action.name=build jar
    init:
    Deleting: C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\build\built-jar.properties
    deps-jar:
    Updating property file: C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\build\built-jar.properties
    Compiling 1 source file to C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\build\classes
    C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\src\chapter\pkg2\exercise\pkg2\pkg30\Chapter2 Exercise230.java:14: error: ']' expected
    int[5] numbers=0;
    C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\src\chapter\pkg2\exercise\pkg2\pkg30\Chapter2 Exercise230.java:14: error: not a statement
    int[5] numbers=0;
    C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\src\chapter\pkg2\exercise\pkg2\pkg30\Chapter2 Exercise230.java:14: error: illegal start of expression
    int[5] numbers=0;
    3 errors
    C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\nbproject\build-impl.xml:923: The following error occurred while executing this line:
    C:\Users\Matt\Documents\NetBeansProjects\Chapter 2 Exercise 2.30\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 1 second)


    And Heres the code I'm working with:

     
    package chapter.pkg2.exercise.pkg2.pkg30;
    import java.util.Scanner;
     
    public class Chapter2Exercise230 {
     
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
     
            System.out.println("Please input a 5 digit number");
     
    	int input = in.nextInt();
            int num1=0, num2=0, num3=0, num4=0, num5=0;
            int[5] numbers=0;
            /*        
            while (input > 0)
            {
                System.out.println(input % 10);
                input = input /10;
            }
            */
     
     
        }
    }

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

    Default Re: Not sure where my problem is

    The Oracle tutorials and the API pages are great references for these annoying syntax and similar questions until you get the hang of them - and move on to more advanced annoyances. Usually, simply Googling "java array" will list the Oracle documentation or tutorial page as the first result.

  9. #9
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    Believe it or not that exact same google search lead me to a page where I got that syntax I was trying to use above. I'll have a look at that web page, could you tell me what i'm looking for more specifically?

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

    Default Re: Not sure where my problem is

    Really? You found something like this:

    int[5] numbers=0; // ? - more of a C statement

    Compare that to the two sections of this page. The first is named "Declaring a Variable to Refer to an Array," and the next.

  11. #11
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    well, the brackets were empty. But, yea.

    If this idea of mine could be done another way w/out the array, then I'd be all ears. But this is the only way I can see to reverse the order the numbers print out in.

    Edit:
    If I'm reading that web page right, I need something like:
    numbers = new int[5];
    . But I'm not sure where to put it. The web page was less then clear on that. Does it go up above with the import statement, or does it go with the variable declarations?
    Last edited by mattig89ch; August 30th, 2014 at 05:07 PM.

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

    Default Re: Not sure where my problem is

    well, the brackets were empty. But, yea.
    Empty for a reason: that's the correct way to declare an array variable in Java.

    Clarify the assignment. Is it to get a 5-digit number from the user and then repeat it backwards? If the user inputs '12345' the program prints '54321'. Is that the assignment? If so, and if that's all there is to do, then an array is not necessary, but clarify first, and we'll go from there.

  13. #13
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    Yep, thats the whole reason for the array. And technically, I don't need to do that. But, if I'm going to be forced to learn a small amount of this language, I might as well pull out all the stops for every homework/lab assignment I have to do (so long as I have time to do it).

    The only other thing I can see to make this program better (asside from reversing the output) is to have some way of checking the amount of numbers the user inputs into the program. So if they put in 4 or 6 numbers, an error message comes back saying something like 'thats too many/few numbers'.

    But one thing at a time. Yes, if a user puts in 12345, I want it to spit out 1 2 3 4 5. Not 5 4 3 2 1

  14. #14
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Not sure where my problem is

    Seems like a rather simple algorithm would work here:

    *declare an integer array with enough allocated space to hold the input
    *read input into the array using a for loop - test after each input if too many too few
    and print error message then ask to re-enter
    *if input is OK - print array contents back using another for loop.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  15. #15
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    oh yea, that was my goal. trouble is, delcaring that array. I can't quite figure out how to declare the array.

  16. #16
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Not sure where my problem is

    Array definition in Java is:

    return type [] nameOfArray
    Then you create an array object
    nameOfArray = new return type[ size ]
    Where size is the total allocated space for the array. Remember unlike C, Java
    does not use the null termination '/0'

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

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

    Default Re: Not sure where my problem is

    Do you really want to do this with numbers? I can see that this probably makes for a better learning experience, but the more simpler way would be to cast the number to a string and to iterate over its characters.
    You could do this with only a few lines of code.

  18. #18
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    See now, I have no idea what you just said cornix. This is the first program that i've written in java in 4 years or so.

    ok, heres what I came up with:
     
    package chapter.pkg2.exercise.pkg2.pkg30;
    import java.util.Scanner;
     
    public class Chapter2Exercise230 {
     
        public static void main(String[] args) 
        {
            Scanner in = new Scanner(System.in);
     
            System.out.println("Please input a 5 digit number");
     
    	int input = in.nextInt();
            //int num1=0, num2=0, num3=0, num4=0, num5=0;
     
            int[] numbers;
     
            numbers = new int[5];
     
            for (int i = 5; i > 0; i--)
            {
     
                numbers[i] = (input % 10);
                input = input /10;
            }
     
            for (int i = 0; i < 5; i++)
            {
                System.out.println(numbers[i]);
            }
     
        }
    }

    It compiles just fine, but when I tried to run it I got this error message:

    run:
    Please input a 5 digit number
    12345
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at chapter.pkg2.exercise.pkg2.pkg30.Chapter2Exercise2 30.main(Chapter2Exercise230.java:23)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 8 seconds)

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

    Default Re: Not sure where my problem is

    An array with a size of 5 can hold exactly 5 elements at the indices 0, 1, 2, 3 and 4.
    You tried to access the index 5, which would be the sixth element in the array, and this index is out of bounds.
    Thats why you got the ArrayIndexOutOfBoundsException.



    My suggestion about the String would involve creating a new Object of type String that would read the number. Surely there are Strings in C++ so I suppose you know what this is.
    Once you have a String object you can iterate over all characters in the String.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not sure where my problem is

    When iterating through an array with a standard for loop, it's better to use the array's .length field and not hard code the size of the array:
      for(int i=0; i<theArray.length; i++) {
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    So instead of starting the loop at 5 I shoud star it at 4, and end it at -1 instead of 0? Do I have that right?

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

    Default Re: Not sure where my problem is

    If it enters the loop with -1 you will get the same exception. The values need to be between 0 and 4 (inclusive). Surely arrays work the same way in C++, dont they?

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not sure where my problem is

    It's better to start at theArray.length-1. That will be the index to the last element in the array.

    It's better NOT to hardcode numbers like array lengths in a program.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where my problem is

    Im confused. Which loop are we talking about? The first one or the second one?

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not sure where my problem is

    Use 0 as the starting index when searching from the first element to the last
    Use theArray.length-1 as the starting index when searching from the last element to the first
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Catch block problem. Please fix my problem.
    By damnitsme in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2014, 01:49 AM
  2. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  3. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  4. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM