d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Question
Add Reply New Topic New Poll
Member
Posts: 14,592
Joined: Dec 11 2007
Gold: 7,978.50
Oct 23 2012 12:46pm
Doing a project for class.
Using DrJava compiler.
We've been using
Code
int x = Integer.parseInt(args[0])
(and double, etc) for user inputs so far. I know this converts a user input string to, let's say an integer. But how do I take user input string and keep it as a string. I thought this would work but it doesn't...
Code
String x = String.parseString(args[0])
(says Error: The method parseString(java.lang.String) is undefined for the type java.lang.String)

I've tried googling this but didn't help.
So I ask, what method do I use that takes user input as a string
For example something like this.
Code
public class program
{
 public static void main(String[] args)
 {
   String x = String.parseString(args[0]); // DOESN'T WORK!!
   System.out.println(x);
 }
}

run program blablabla
output: blablabla


I suck at programming, dont blame me, plz help.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 23 2012 12:55pm
Read the compiler error to yourself.

"Error: The method parseString(java.lang.String) is undefined for the type java.lang.String"

You are trying to invoke the method parseString(java.lang.String) on the class java.lang.String. If you look you'll find that there isn't a parseString method that takes a String as a parameter. That's the error. The value of args[0] is already a string, so just assigning it will suffice.
Member
Posts: 14,592
Joined: Dec 11 2007
Gold: 7,978.50
Oct 23 2012 12:57pm
Quote (rockonkenshin @ Oct 23 2012 06:55pm)
Read the compiler error to yourself.

"Error: The method parseString(java.lang.String) is undefined for the type java.lang.String"

You are trying to invoke the method parseString(java.lang.String) on the class java.lang.String. If you look you'll find that there isn't a parseString method that takes a String as a parameter. That's the error. The value of args[0] is already a string, so just assigning it will suffice.


Then what method do I use to get a program like this working

Code
public class program
{
 public static void main(String[] args)
 {
   String x = String.parseString(args[0]); // something else that works
   System.out.println(x);
 }
}

run program blablabla
output: blablabla


This post was edited by Molek on Oct 23 2012 12:57pm
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 23 2012 12:58pm
Quote (Molek @ Oct 23 2012 01:57pm)
Then what do I have to do to get a program like this working

Code
public class program
{
 public static void main(String[] args)
 {
   String x = String.parseString(args[0]); // something else that works
   System.out.println(x);
 }
}

run program blablabla
output: blablabla


I just told you how. args[0] is already a String, for one. For two you are trying to call a method on String that doesn't exist. Just assign args[0] to x and it will work. Do you know why that is?
Member
Posts: 14,592
Joined: Dec 11 2007
Gold: 7,978.50
Oct 23 2012 01:02pm
Quote (rockonkenshin @ Oct 23 2012 06:58pm)
I just told you how. args[0] is already a String, for one. For two you are trying to call a method on String that doesn't exist. Just assign args[0] to x and it will work. Do you know why that is?


Oh, you're right sorry. It worked! Thanks a lot.

And yes I believe I know now why it works ; )
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 23 2012 01:16pm
Quote (Molek @ Oct 23 2012 02:02pm)
Oh, you're right sorry. It worked! Thanks a lot.

And yes I believe I know now why it works ; )


Awesome. Reading compiler errors gets easier with time but it's really important that you get used to reading them and understanding what it's trying to tell you.
Member
Posts: 9
Joined: Nov 7 2012
Gold: 0.00
Nov 7 2012 09:40pm
What's really fun is when you get to the level of comprehension where you can begin to study the low level parts of the jvm such as bytecode instructions! things start to make a lot more sense once you understand how javac and the jvm work together, but it takes a bit of knowledge and understanding to be able to look at that sort of thing and not completely mind screw yourself!
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 7 2012 09:48pm
holy necro batman
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll