methods - How do I reassign a scanner-based value that was already entered? - Java -


not seeing posts able answer question with, ask myself. trying figure out programming , had intent of making simple program ask user input integer. after entering whatever user had entered, if variable, in following line of output. if user had not entered integer, catch error , ask user attempt reenter integer--this process repeating until integer entered , program terminate.

generally having low level of experience above straight-forward programming , having successful, code not has more errors above problem having, have errors didn't know there.

i have tried quite lot of different things, , have of idea of going on other blocks, here right now:

import java.util.scanner;  public class launchermain {      static scanner reader = new scanner(system.in);      public static void setinputvalue() {         int userinput = reader.nextint();     }      public static void tryinputvalue(int userinput) {       //do need parameter, seems not have correlation userinput integer defined above         try {             setinputvalue();         }         catch (java.util.inputmismatchexception userinput) {    //if code in method accept using userinput, goes on duplicate variable             system.out.println("that wasn't integer!");      //i haven't looked it, warning instead of basic output             setinputvalue();    //i don't think right either, have call tertiary method calls try again? ...but have (necessary) block         }         {             system.out.println("awesome, value now: " + userinput);         }     }      public static void main(string[] args) {         system.out.println("enter integer:");         tryinputvalue(int userinput);   //looks getting sort of error down here, has not being able resolved variable     }  } 

alright, if managed survive reading through wrong code, main problem getting getting user able reenter integer after failing previous attempt. had before trying add block work, , additionally, not crash if entered not integer, because catch block there. problem was stuck falsely entered value , therefore not use variable: userinput input mismatch. after attempting add in allow reassigned, started throwing errors, began changing stuff, , getting errors in different places not understand.

if willing help, thank tons! , if willing, description of doing in fix provide incredibly useful! :)

when call int userinput = reader.nextint();, userinput varable defined in scope of setinputvalue() method, there no way access input after entered.

you either make userinput instance variable, or return value setinputvalue() method.

the duplicate variable error you're getting on catch (java.util.inputmismatchexception userinput) { because have userinput defined parameter in tryinputvalue() method. name exception else, (java.util.inputmismatchexception e)

you need add loop in order read input inside try/catch block, otherwise not catch exception if input invalid.

here 1 way go fixing errors , getting working:

import java.util.scanner;  public class launchermain {      static scanner reader = new scanner(system.in);      //int return value     public static int setinputvalue() {         int userinput = reader.nextint();         return userinput; //return value entered     }      //use int return value access value in main     public static int tryinputvalue() {         int userinput = 0; //initialize variable store input               boolean valid = false; //boolean enable looping until valid input entered         while (valid == false){                 try {             userinput  = setinputvalue(); //get return value             valid = true;  //set valid true if no exception thrown           }           catch (java.util.inputmismatchexception e) {    //remove duplicate                         system.out.println("that wasn't integer!");                              }           {            //nothing needed here           }         }          system.out.println("awesome, value now: " + userinput); //this work now, since userinput in scope          return userinput;     }      public static void main(string[] args) {         system.out.println("enter integer:");         int n = tryinputvalue();   //no parameter, return value           system.out.println(n);   //you have access number user entered     }  } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -