2

Okay so i'm trying to convert a couple things.

So i have already converted my Scanner to a String, now what I want to do is, take the value that they input and use it as an integer for a couple else if statements. IT WONT WORK!

Here is my code so far:

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

    Scanner fname = new Scanner(System.in);
    Scanner sname = new Scanner(System.in);
    Scanner number = new Scanner(System.in);
    tuna weight = new tuna();

    System.out.println("Enter Your First Name: ");
    String fname1 = fname.nextLine();
    String fnames = fname1;

    System.out.println("Enter Your Last Name: ");
    String sname1 = sname.nextLine();
    String snames = sname1;

    System.out.println("Enter Your Weight (Lbs.) : ");
    String num = number.nextLine();
    String num1 = num;

    System.out.println("Okay " + fname1 + " " + sname1
            + " I can see here that you weigh " + num + "lbs.");
    int num2 = num1.parseInt();
    if (num1 >= 275)
        System.out
                .println("You know, you should maybe consider laying off the candy my friend.....");
}

}

1
  • 1
    As a general comment you don't need three scanners. You would get the same results if you used one scanner and called it three times. This would also mean that you wouldn't have the similar variable names of fname being a scanner and fname1 being a string Commented Aug 29, 2012 at 23:26

5 Answers 5

5

You should use an argument in parseInt:

int num2 = Integer.parseInt(num1);

and

if (num2 >= 275) 
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton man! I was using the parseInt but didnt know about the (num1); part. Thanks again!
2

Try this:

try{
  num2 = Integer.parseInt(num1);
}
catch(Exception ex) {
  System.out.println("Something went wrong, the string could not be converted to int.");
}

the try-catch is pretty important because the string could contain characters that cannot convert to int, ofc you need to catch it better than this, but keep it in mind

1 Comment

+1 for the exception handling (although I'd prefer a ParseException ;))
0

You can use other type scanner method also to get the int value

num = number.nextInt();

Comments

0

Here is what I did:

Scanner input = new Scanner(System.in);
System.out.println("Type number");
int number = input.nextInt();
System.out.println(number);

Hope this helps...

Comments

-1

I'm not a pro in Java myself but I tweaked your code up a notch to get it working. Here it is. Glad if it helps.

p

ackage APPLE;
import java.util.Scanner;
public class apples {
public static void main(String args[]) {

    Scanner fname = new Scanner(System.in);
    Scanner sname = new Scanner(System.in);
    Scanner number = new Scanner(System.in);
    Scanner intScanner = new Scanner(System.in);

    System.out.println("Enter Your First Name: ");
    String fname1 = fname.nextLine();
    String fnames = fname1;

    System.out.println("Enter Your Last Name: ");
    String sname1 = sname.nextLine();
    String snames = sname1;

    System.out.println("Enter Your Weight (Lbs.) : ");
    int num = intScanner.nextInt();


    System.out.println("Okay " + " " + fname1 + " " + sname1 
            + " I can see here that you weigh " + num + "lbs.");

    if (num > 275){
        System.out.println("You know, you should maybe consider laying off the candy my friend.....");
    }
}
}

1 Comment

What's the purpose of answering a question that was both asked and satisfactorily answered three years ago? And to top it off, you didn't even bother to explain the changes you made that caused you to consider it "up a notch."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.