Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: type conversion error

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation type conversion error

    Hi I am getting a Type mismatch: cannot convert from java.lang.String to int error. How do I convert between those types?

    package Spiller;
    import java.util.Scanner;
     
     
    public class Spiller {
     
      private String navn;
      private String adresse;
      private int postNummer;
      private String poststed; 
     
      public Spiller(String initNavn, String initAdresse, int initPostnummer, String initPoststed) {
     
       navn = initNavn;
       adresse = initAdresse;
       postNummer = initPostnummer;
       poststed = initPoststed;
      }
     
     
      public void hentNavn() {
     
     
     
        Scanner myScanner = new Scanner (System.in);
        String input1;
     
        System.out.println("Hva heter du?");
        input1 = myScanner.nextLine();
        navn = input1;
      }
     
     
     public void hentAdresse() {
     
        Scanner myScanner = new Scanner (System.in);
        String input2;
     
        System.out.println("Oppgi adresse: ");
        input2 = myScanner.nextLine();
        adresse = input2;
      }
     
     public void hentPostnummer() {
     
    Scanner myScanner = new Scanner (System.in);
        String input3;
     
        System.out.println("Oppgi postnummer: ");
        input3 = myScanner.nextLine();
        postNummer = input3;
     }
     
      public void hentPoststed() {
     
       Scanner myScanner = new Scanner (System.in);
        String input4;
     
        System.out.println("Oppgi poststed: ");
        input4 = myScanner.nextLine();
        poststed = input4;
     }
     
    }

    Thanx


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: type conversion error

    String myString = "42";
    int myInt = Integer.parseInt(myString);

Similar Threads

  1. Unchecked conversion error
    By Blehs in forum Java Theory & Questions
    Replies: 5
    Last Post: August 10th, 2011, 03:42 AM
  2. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  3. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 05:26 PM
  4. BASE CONVERSION
    By bgwilf in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 13th, 2010, 12:02 PM
  5. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM