Torstein Norum Bugge Torstein Norum Bugge - 1 year ago 71
Java Question

Java input not being interpreted correctly

I'm trying to make a program that allows the user to input first his or her name ("navn"), and second his home ("bosted"). The result should then be printed as in the bottom line: "Hei, John Doe! Du er fra Chicago". Just an example.

The code compiles and runs fine, but crashes at the input of the name. There is no error, but something else pops up:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Torstein"
at java.lang.NumberFormatException.forInputString(
at java.lang.Integer.parseInt(
at java.lang.Integer.parseInt(
at Metoder.main(

What can I do to make it run correctly?

import java.util.Scanner;

public class Metoder
public static void main (String[] args)
Scanner tastatur = new Scanner(;

System.out.println ("Skriv inn navn:");
String in1 = tastatur.nextLine();
int navn = Integer.parseInt(in1);

System.out.println ("Skriv inn bosted:");
String in2 = tastatur.nextLine();
int bosted = Integer.parseInt(in2);

System.out.println ("Hei, " + navn + ("! ") + ("Du er fra ") + bosted);


Answer Source
    import java.util.*;

import javax.print.attribute.standard.PrinterLocation;


public class test{
public static void main(String[]args){
    //This takes input for the name.
     Scanner kb=new Scanner(;
     System.out.println("Enter your name.");
     String name=kb.nextLine();

     //This takes input for the city
     System.out.println("Enter the city.");
     String location=kb.nextLine();
     System.out.println("Hi "+name+" from "+location);

So if I understood correctly you want to input 2 things and display them in the line below correct? I got a little confused with some of the variable so i reworte it. If you have any questions let me know.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download