Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,195,038 members, 7,956,863 topics. Date: Monday, 23 September 2024 at 08:57 PM

Help! Where Did I Go Wrong? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help! Where Did I Go Wrong? (3363 Views)

I Want My Son To Be A Programmer, How Do I Go About It / Urgent Help: Where To Download Udemy Courses / Should I Go For Cybersecurity Or Computer Science? (2) (3) (4)

(1) (2) (Reply) (Go Down)

Help! Where Did I Go Wrong? by HealerC: 6:38pm On Nov 28, 2017
I started learning java not quite long but now I am faced with this problem in the screenshot.
I don't know why it happens like this. Please someone should proffer a solution. Thanks.

Re: Help! Where Did I Go Wrong? by Nobody: 7:12pm On Nov 28, 2017
You are multiplying String with Integer why not cast to int before doing that, read a proper book
Re: Help! Where Did I Go Wrong? by HealerC: 10:50pm On Nov 28, 2017
pcguru1:
You are multiplying String with Integer why not cast to int before doing that, read a proper book
Thank you pcguru1 for your swift response. According to what I read, "you can assign char values to int variables". But neverthrless I did what u suggested. See The same thing

Re: Help! Where Did I Go Wrong? by HealerC: 10:52pm On Nov 28, 2017
Or could you please give me an alternative in getting single integer values from integers without first converting to string and using charAt?
Re: Help! Where Did I Go Wrong? by Nobody: 11:02pm On Nov 28, 2017
Stop wasting your time go and read a Java book that's not how you cast string to it, you use the Integer class check the Javadoc API read the fundamentals and data types for a start , Charat returns a String which is why Interger classes were created though am interested how that number came to be. I might try to find out tomorrow
Re: Help! Where Did I Go Wrong? by Nobody: 11:03pm On Nov 28, 2017
HealerC:
Or could you please give me an alternative in getting single integer values from integers without first converting to string and using charAt?

Integer
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
Re: Help! Where Did I Go Wrong? by Nobody: 11:14pm On Nov 28, 2017
So I hate being curious I was intrigued as to why you got 1323 so when you say Charat it fetched the index 0 which points to 1 however in this case '1' and the fact that you used Interger you forced char to be converted to decimal in which in the Ascii table that's a 49 and you multiplied that by 27 which gave you your result. You're lucky you didn't exceed 256 past the size of the char I don't think it would have been a result you'd be able to explain.

Re: Help! Where Did I Go Wrong? by Nobody: 11:20pm On Nov 28, 2017
255 sorry not 256
Re: Help! Where Did I Go Wrong? by HealerC: 7:10am On Nov 29, 2017
pcguru1:
So I hate being curious I was intrigued as to why you got 1323 so when you say Charat it fetched the index 0 which points to 1 however in this case '1' and the fact that you used Interger you forced char to be converted to decimal in which in the Ascii table that's a 49 and you multiplied that by 27 which gave you your result. You're lucky you didn't exceed 256 past the size of the char I don't think it would have been a result you'd be able to explain.
WTF lipsrsealed
Re: Help! Where Did I Go Wrong? by HealerC: 7:11am On Nov 29, 2017
pcguru1:
255 sorry not 256
Thank you very much bros
Re: Help! Where Did I Go Wrong? by Nobody: 7:14am On Nov 29, 2017
HealerC:

WTF lipsrsealed

ASCII when memory was scare and highest was 2 bytes so they thought of smart ways to hold data.

1 Like

Re: Help! Where Did I Go Wrong? by HealerC: 7:19am On Nov 29, 2017
pcguru1:


Integer
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
Sorry to disturb you but I dont have much experience in reading documentations yet. embarassed

Could you point it to me, please? embarassed
Re: Help! Where Did I Go Wrong? by Nobody: 7:21am On Nov 29, 2017
Integer.valueof you also need to learn reading Java Docs and OOP too, working on enterprise systems mehn you will need to, and Java docs is anything but straight forward
Re: Help! Where Did I Go Wrong? by HealerC: 7:41am On Nov 29, 2017
pcguru1:
Integer.valueof you also need to learn reading Java Docs and OOP too, working on enterprise systems mehn you will need to, and Java docs is anything but straight forward
Thank you so much, I'll try it.
Whats enterprise system¿
Re: Help! Where Did I Go Wrong? by HealerC: 7:44am On Nov 29, 2017
pcguru1:


ASCII when memory was scare and highest was 2 bytes so they thought of smart ways to hold data.
Why Didnt they just leave it as 1 angry
Re: Help! Where Did I Go Wrong? by Nobody: 8:06am On Nov 29, 2017
HealerC:

Thank you so much, I'll try it.
Whats enterprise system¿
Ignore I said that
Re: Help! Where Did I Go Wrong? by Nobody: 8:07am On Nov 29, 2017
HealerC:

Why Didnt they just leave it as 1 angry

1 and '1' are not the same go and read data type , you can't question a bunch of smart researchers who built this ,because its what the world runs on, they'd prob have a smart reason for that.
Re: Help! Where Did I Go Wrong? by sunsyde(m): 10:42am On Nov 29, 2017
use Integer.parseInt (g.charAt(m)) to cast from "1" to 1
Re: Help! Where Did I Go Wrong? by Nobody: 11:21am On Nov 29, 2017
babawarunn:
As pcguru1 said. Go and read a good java book. I recommend Java How to program.

This is the solution you need if you want to go about it the way you have written your code.

System.out.println(Integer.parseInt(String.valueOf(g.charAt(m))) * (int) Math.pow(b, a));

g.chartAt(m) : returns char '1'

String.valueOf('1') : returns String "1"

Integer.parseInt("1"wink : returns integer 1

1 * 27 = 27.

There is no shortcut. Take time to read a good java book, you will avoid mistakes like this.


Thanks I realized I sent value of instead of parseInt
Re: Help! Where Did I Go Wrong? by HealerC: 5:54am On Nov 30, 2017
pcguru1:


Thanks I realized I sent value of instead of parseInt
babawarunn:
As pcguru1 said. Go and read a good java book. I recommend Java How to program.

This is the solution you need if you want to go about it the way you have written your code.

System.out.println(Integer.parseInt(String.valueOf(g.charAt(m))) * (int) Math.pow(b, a));

g.chartAt(m) : returns char '1'

String.valueOf('1') : returns String "1"

Integer.parseInt("1"wink : returns integer 1

1 * 27 = 27.

There is no shortcut. Take time to read a good java book, you will avoid mistakes like this.

sunsyde:
use Integer.parseInt (g.charAt(m)) to cast from "1" to 1
pcguru1:


1 and '1' are not the same go and read data type , you can't question a bunch of smart researchers who built this ,because its what the world runs on, they'd prob have a smart reason for that.
pcguru1:

Ignore I said that
Thank you all so very much
But is there a way to just do it automatically like, for instance if u have a string "story" u can get charat index 1 using charAt1 to get t
so is there a way for instance integer 1234 to get just a particular integer let's say 3. WITH IT STILL BEING AN INTEGER. something like that, please
Re: Help! Where Did I Go Wrong? by HealerC: 5:57am On Nov 30, 2017
Supposing I want to make a basic cmd program that will solve number bases and convert them to base 10
Re: Help! Where Did I Go Wrong? by Nobody: 6:05am On Nov 30, 2017
HealerC:

Thank you all so very much
But is there a way to just do it automatically like, for instance if u have a string "story" u can get charat index 1 using charAt1 to get t
so is there a way for instance integer 1234 to get just a particular integer let's say 3. WITH IT STILL BEING AN INTEGER. something like that, please

An integer is a decimal charat is for string , that would mean you convert int to String and use a char at and convert back to Int but that's an odd operation what's the purpose for ?
Re: Help! Where Did I Go Wrong? by HealerC: 6:24am On Nov 30, 2017
pcguru1:


An integer is a decimal charat is for string , that would mean you convert int to String and use a char at and convert back to Int but that's an odd operation what's the purpose for ?
Any other function apart from charAt?
Re: Help! Where Did I Go Wrong? by Nobody: 6:26am On Nov 30, 2017
HealerC:

Any other function apart from charAt?

Dude seriously just stop and go and read a proper book, This is hand holding, do some research and stuff , WHat i said suffices already just go and read a book, it will save you all this question asking.
Re: Help! Where Did I Go Wrong? by HealerC: 6:28am On Nov 30, 2017
pcguru1:


Dude seriously just stop and go and read a proper book, This is hand holding, do some research and stuff , WHat i said suffices already just go and read a book, it will save you all this question asking.
Thank you very much sir
But a book can't possibly contain ALL functions
Re: Help! Where Did I Go Wrong? by Nobody: 6:50am On Nov 30, 2017
HealerC:

Thank you very much sir
But a book can't possibly contain ALL functions

No but it will teach you how to read JavaDocs especially when you realize everything is an Object and there's a class that handles what you need. if you had read a book you would come across the things needed for what you are asking, parsing String as Int, because your question clearly shows you need to even understand Computer Data types first before even java

Note: Methods i don't think function is the right word though
Re: Help! Where Did I Go Wrong? by HealerC: 3:05pm On Nov 30, 2017
pcguru1:


No but it will teach you how to read JavaDocs especially when you realize everything is an Object and there's a class that handles what you need. if you had read a book you would come across the things needed for what you are asking, parsing String as Int, because your question clearly shows you need to even understand Computer Data types first before even java
Yeah.... even that parse integer method (Integer.parseInt([typechar])) you guys gave me compiled well but when I executed it it gave NumberFormatException embarassed

Perhaps I will have to learn Javadocs.... thah na later sha.... I may soon pause learning java to concentrate on other things

Thanks for your input so far, twas an eye opener

grin
pcguru1:


Note: Methods i don't think function is the right word though
cry cry cry
Re: Help! Where Did I Go Wrong? by jidez007: 4:22pm On Nov 30, 2017
Please kindly use an IDE or a proper code editor. It's a Nigerian mentality that will tell you to use notepad first because you are a beginner.

You still need to be to able to tell what line you are making a mistake, distinct colours for code keywords are important, then you will also be able to properly account for code indentation.
Re: Help! Where Did I Go Wrong? by Nobody: 6:46pm On Nov 30, 2017
HealerC:

Yeah.... even that parse integer method (Integer.parseInt([typechar])) you guys gave me compiled well but when I executed it it gave NumberFormatException embarassed

Perhaps I will have to learn Javadocs.... thah na later sha.... I may soon pause learning java to concentrate on other things

Thanks for your input so far, twas an eye opener

grin

cry cry cry

A NumberFormat Exception means you tried to convert a non numerical to an integer e.g "S" to a number
Re: Help! Where Did I Go Wrong? by asalimpo(m): 9:23pm On Dec 01, 2017
You are using notepad for coding ... You remind me of me when i first started.
Your problems will multiply going that route.
Get an ide or a java programming text editor like notepad++.
Download the documentation for your language. It's essential.
It seems your arent learning in a structured disciplined manner but just trying things out - maybe using web snippets or youtube videos. Get a good book- and slug through it,that way you wont come here asking
redundant questions.
Re: Help! Where Did I Go Wrong? by Nobody: 3:30am On Dec 02, 2017
asalimpo:
You are using notepad for coding ... You remind me of me when i first started.
Your problems will multiply going that route.
Get an ide or a java programming text editor like notepad++.
Download the documentation for your language. It's essential.
It seems your arent learning in a structured disciplined manner but just trying things out - maybe using web snippets or youtube videos. Get a good book- and slug through it,that way you wont come here asking
redundant questions.

exactly which makes it more annonying, these are question you can google and solve, I think dev of this gen have too much hand-holding, when i was learning programming Stack Overflow and Programming communities didn't exist.

(1) (2) (Reply)

Can You Develop Applications With Your Smartphone? / Why You Should Probably Drop Out Of A Nigerian Uni And Learn To Code. / A Thread For Nigerian Cloud Engineers - Prospective And Practising

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.