String to int and int to String

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 482 Views - Last Post: 01 February 2013 - 12:16 PM Rate Topic: -----

#1 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

String to int and int to String

Posted 01 February 2013 - 11:16 AM

Hello everybody, I'm trying to write a very basic, simple code that will "talk" to you, based on your answers
The problem here is, when I'm trying to use if it gives me an error, I understand that is because variable is a String but how can I solve this?
Here's my code

First class
import java.util.Scanner;
public class ap {
	
	public static void main(String[] args){

	Scanner input = new Scanner(System.in);
	
	
	Tuna tunaObject = new Tuna();
	salmon salmon1Object = new salmon();
	
System.out.println("What is your name? ");
String name = input.nextLine();
tunaObject.simpleMessage(name);
String salmon1 = input.nextLine();
salmon1Object.Message(salmon1);





Second class
public class Tuna {
	public void simpleMessage(String name){
		System.out.println("Hello "  + name );
		System.out.println("How old are you?");


		}
}


and third class
public class salmon {
public void Message(String salmon){
if(salmon<0)
{
	System.out.println(salmon+ "? That is impossible");
	
}

}}



Is This A Good Question/Topic? 0
  • +

Replies To: String to int and int to String

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,745
  • Joined: 19-March 11

Re: String to int and int to String

Posted 01 February 2013 - 11:20 AM

You want to use Integer.parseInt()

This is a simple method that takes a String and tries to convert it to an int.

For example, Integer.parseInt("56"); will return 56.
If it fails, though, for example if someone enters their age as "Twenty-seven", it'll throw an Exception, so you might want take this as a good excuse to read up on exception handling.

To go the other way, the simplest thing is to force the int to be interpreted in a String context. You can do this a couple of ways:

System.out.println(age);


will work.

Or
String ageAsString = ""+age;  //concatentate int to String produces a String



Or if you really want you can do
String ageAsString = String.valueOf(age);


but that's not very idiomatic.

This post has been edited by jon.kiparsky: 01 February 2013 - 11:26 AM

Was This Post Helpful? 1
  • +
  • -

#3 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: String to int and int to String

Posted 01 February 2013 - 11:24 AM

I see, I'm quite new to Java and programming in general, as you have probably noticed so sorry for silly questions, but do I use integer.parseInt()
Say, I want to convert a string called "salmon" to int.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,745
  • Joined: 19-March 11

Re: String to int and int to String

Posted 01 February 2013 - 11:27 AM

int foo = Integer.parseInt(salmon);


When in doubt, try it out. Your compiler will tell you if it doesn't work.
Was This Post Helpful? 1
  • +
  • -

#5 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: String to int and int to String

Posted 01 February 2013 - 11:29 AM

Thank you, I understand now :)
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: String to int and int to String

Posted 01 February 2013 - 11:34 AM

Also try to get into the habit of naming your classes properly. You want to pick a name that relates to what the class is/does. Tuna and salmon won't get you far in that respect.

Of course it doesn't matter so much in a small program like this, but it's a good habit to get into :)
Was This Post Helpful? 2
  • +
  • -

#7 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: String to int and int to String

Posted 01 February 2013 - 11:38 AM

Thanks Ryano121, I'll keep that in mind, meanwhile I have another small problem
I'm trying to get following outpot
"You were born in X and your name is Y"
I'm trying to write the following code
System.out.println("You were born in " + foo "and your name is" + tuna); 

But it doesn't seem to work, Why?
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: String to int and int to String

Posted 01 February 2013 - 11:42 AM

What are the types of 'foo' and 'tuna'.

If you simply try and print out an object reference (like a Tuna), you will get a random string back (well it's not really random but thats a topic for another day). On the other hand if you print out a String object (it acts a bit different), it will give you what you expect - the actual string it holds.

If you above code is anything to go by I think you want to do

System.out.println("You were born in " + foo "and your name is" + name);


But I don't have much to go on. Post your updated code.
Was This Post Helpful? 0
  • +
  • -

#9 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: String to int and int to String

Posted 01 February 2013 - 11:55 AM

Yes, that is what I was actually trying to write, sorry about the tuna part.
Here's what I'm trying to do
When user inputs his age that is..let's say 16
I have created another variable called answer which is
answer = 2013 - foo;

Which does work, however I'm trying to get output that is:
"You were born in 1997(2013 - foo) and your name is (String variable called name)"
When I'm entering this code

System.out.println("You were born in " + foo "and your name is" + name);


It gives me an error
Posted Image
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,745
  • Joined: 19-March 11

Re: String to int and int to String

Posted 01 February 2013 - 11:58 AM

View Postam0n, on 01 February 2013 - 01:55 PM, said:

System.out.println("You were born in " + foo "and your name is" + name);


It gives me an error

You're missing a + sign in that expression:


System.out.println("You were born in " + foo + "and your name is" + name);

Was This Post Helpful? 1
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: String to int and int to String

Posted 01 February 2013 - 11:59 AM

In that case you are missing a '+' and I think you want to use 'answer' instead then?

System.out.println("You were born in " + answer + "and your name is" + name);


Also it seems as though you are trying to access the name variable in the salmon class. Remember that 'name' is a local variable - you can only access it in the method that it was declared in - nowhere else. If you want to use it in the method inside the salmon class, you will need to pass it as a parameter, or probably better just do the println in the same method as name is declared.

Hope that makes sense
Was This Post Helpful? 1
  • +
  • -

#12 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: String to int and int to String

Posted 01 February 2013 - 12:04 PM

View Postjon.kiparsky, on 01 February 2013 - 11:58 AM, said:

View Postam0n, on 01 February 2013 - 01:55 PM, said:

System.out.println("You were born in " + foo "and your name is" + name);


It gives me an error

You're missing a + sign in that expression:


System.out.println("You were born in " + foo + "and your name is" + name);

Now the "name" has red line under it, instead of foo AND name
.

It makes sense, Ryano, I'm trying to get output of 2 variables that was declared in two different classes, how do I achieve this?
Was This Post Helpful? 0
  • +
  • -

#13 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: String to int and int to String

Posted 01 February 2013 - 12:09 PM

Normally through the magic of parameters.

I think its best that you post your full updated code again. Everyone is getting confused with all the new variable names appearing everywhere.
Was This Post Helpful? 0
  • +
  • -

#14 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-January 13

Re: String to int and int to String

Posted 01 February 2013 - 12:11 PM

First class
 import java.util.Scanner;
public class ap {
	
	public static void main(String[] args){

	Scanner input = new Scanner(System.in);
	
	
	Tuna tunaObject = new Tuna();
	salmon salmon1Object = new salmon();
	
System.out.println("What is your name? ");
String name = input.nextLine();
tunaObject.simpleMessage(name);
String salmon1 = input.nextLine();
salmon1Object.Message(salmon1);




        	
         }}

Second class
public class Tuna {
	public void simpleMessage(String name){
		System.out.println("Hello "   + name);
		System.out.println("How old are you?");


		}
}


Third class
public class salmon {
public void Message(String salmon){
	int foo = Integer.parseInt(salmon);
	
	int answer;
	if(foo<0)
	{
		
		System.out.println(foo+ "? This is impossible");
	}
if(foo>=1 && foo<=10)
{
	System.out.println(foo+ " ? You're too young");
}

if(foo>10 && foo<60)
{
	
	answer = 2012 - foo;
	System.out.println("You were born in " + answer + "and your name is" + name);
	
	
	
}
}}



I'm very new to Java, as stated before, is what I'm asking beginner material or slightly more advanced?
Was This Post Helpful? 0
  • +
  • -

#15 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: String to int and int to String

Posted 01 February 2013 - 12:15 PM

Yeah you are defining the name variable in your main method meaning that is only accessible in that method. Therefore the salmon.Message method is complaining that name doesn't exist as well it can't see it.

Best solution would be to pass the name in as another parameter to the Message method -

public void Message(String salmon, String name)
{
 // you can now use name in here
}


called by

salmon1Object.Message(salmon1, name);

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2