Results 1 to 4 of 4
  1. #1
    Christopher is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Unhappy Whileloop is going crazy! help

    so I'm trying do make this highscore program... at some point in the program I ask the user to insert a name and value so I can pass it to another class that adds what I wrote to a textfile but
    the program wont let me input 2 times it just loops again :S here:


    public static void main(String[] args) throws IOException{
    Scanner input = new Scanner(System.in);
    int choise =0;
    String name;
    int score;
    person one = new person();
    addplayer yes = new addplayer();
    while(true){
    one.enterfromlist();
    try{
    choise = input.nextInt();
    }
    catch(Exception e){
    System.out.println("not a number!");
    input.nextLine();
    }

    if(choise == 1){
    System.out.println("Enter your score and Name");
    try{
    score = input.nextInt();
    name = input.nextLine();

    yes.give(score, name);
    }catch(Exception a){
    System.out.println("not a number!");
    }

    }
    else if(choise == 2){
    }
    else if(choise == 3){
    }
    else if(choise == 4){
    }
    else{
    System.out.println("Enter a valid number");
    }
    }
    //----------------------------new classs below
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;


    public class addplayer {
    addplayer(){

    }



    void give(int score, String name) throws IOException{
    FileWriter apa = new FileWriter("D:\\Te buddy\\eclipse\\text.txt", true);
    PrintWriter outputFile = new PrintWriter(apa);
    outputFile.println(score);
    outputFile.println(name);

    outputFile.close();

    }
    }

  2. #2
    Christopher is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Whileloop is going crazy! help

    fixed it :D

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: Whileloop is going crazy! help

    Great! But you should explain what you did so others may benefit.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Christopher is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Cool Re: Whileloop is going crazy! help

    :) okey so what I did was to reset the scanner with adding this line: "input.nextLine();" where "input" is the scanner.

    if(choise == 1){
    System.out.println("Enter your score and Name");
    try{
    input.nextLine();
    score = input.nextInt();
    input.nextLine();
    name = input.nextLine();
    yes.give(score, name);
    }
    catch(InputMismatchException ead){

    }

Similar Threads

  1. Crazy Number Format
    By SamJava_the_Hut in forum New To Java
    Replies: 7
    Last Post: 10-12-2013, 05:18 AM
  2. Driving me crazy!!!! Please help!!
    By Quizzle23 in forum New To Java
    Replies: 33
    Last Post: 02-24-2011, 08:00 AM
  3. A crazy gui match
    By amarenash23 in forum New To Java
    Replies: 8
    Last Post: 12-30-2009, 03:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •