CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Jaisingh
Level 14
Hyderabad
  • 01.10.2018
  • 1364views
  • 7comments

as i expected that the getStackTrace() gives us the array of stackElements so i used an count variable to count and then display it... but it goes wrong . please do tell me whats wrong with it

Question about the task In the blue depths of the stack trace…
Java Syntax,  Level 9,  Lesson 2
Under discussion


Write a method that returns the stack trace depth, i.e. the number of methods in the stack trace.
The method should display this same number on the screen.

Requirements:
  • The getStackTraceDepth method must return the depth of its stack trace.
  • The getStackTraceDepth method should display the depth of its stack trace.
  • Use Thread.currentThread().getStackTrace().
  • The main method must call the getStackTraceDepth method.
package com.codegym.task.task09.task0905; /* In the blue depths of the stack trace… */ public class Solution { public static void main(String[] args) throws Exception { int deep = getStackTraceDepth(); System.out.println(deep); } public static int getStackTraceDepth() { // write your code here StackTraceElement[] ele = Thread.currentThread().getStackTrace(); int i = 0; for (StackTraceElement element : ele) { i++; } return i; } }
0
Comments (7)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Jaisingh
Level 14 , Hyderabad, India
1 October 2018, 07:40
when do we use length and length(). ?
0
Ankush Rajput
Level 25 , Bhopal, India
1 October 2018, 08:02
length is used only in case of array. length() is used in case you want to find out the length of a particular string.
0
Jaisingh
Level 14 , Hyderabad, India
1 October 2018, 07:39
yeh i did that . and got it. but it is also supposed to work right ?
0
Ankush Rajput
Level 25 , Bhopal, India
1 October 2018, 08:06
Yes, whether you use for loop or directly return using .length function, both will work. .length function will make your code look short and more readable.
0
Ankush Rajput
Level 25 , Bhopal, India
1 October 2018, 07:25
return (i-1) instead of i. If you return i, getStackTrace method also gets counted which shouldn't be counted. It was only used to get the stackTrace and wasn't part of originally functioning program. One more suggestion, as Learner pointed out, instead of using for loop, you can simply do ele.length. It will give you length of the array. return (ele.length - 1);
0
ozayr ahmed
Level 11 , Paterson, United States
29 November 2018, 18:33
You don't have to add the -1 at the end. Simply returning ele.length is enough.
0
Learner
Level 16 , India
1 October 2018, 06:56
I guess you can use .length to get an array size instead of iterating the count in for loop.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.