logo
down
shadow

Why does my scanner repeat?


Why does my scanner repeat?

By : user2956479
Date : November 22 2020, 10:56 AM
To fix the issue you can do I'm trying to make a GPA calculator but early on in the code I started having issues; here's the code segment: , You can fix this by adding
code :
input.nextLine();
int length = input.nextInt();
Scanner input = new Scanner(System.in);

System.out.println("How many grades are you putting? ");
int length = input.nextInt();
input.nextLine();

String[] gradesArray = new String[length];

for(int i = 0; i < gradesArray.length; i++)
{
    System.out.println("Enter grade (include + or -) ");
    gradesArray[i] = input.nextLine();
} 


Share : facebook icon twitter icon
My scanner/terminal window stops functioning after one repeat

My scanner/terminal window stops functioning after one repeat


By : knowless
Date : March 29 2020, 07:55 AM
Hope this helps I am currently working on a tower defense project on gridworld (not sure if that is significant). I have a timer that calls a method (TowerPlacer) from my main class to receive user text input via scanner. However, after the first time through, the terminal window stops allowing me to input text despite the TowerPlacer timer running. , The mistake is in your TimerTask. What you need is:
code :
class SayHello extends TimerTask {

    public void run()
    {
      System.out.println("done");
    }

 }
public static void main(String[] args)
{
    int delay = 2000; // delay for 2 seconds
    int period = 500; // repeat every .5 seconds

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new SayHello(), delay, period);
}
how to repeat loop based on user selection using scanner in java

how to repeat loop based on user selection using scanner in java


By : user2363222
Date : March 29 2020, 07:55 AM
This might help you Hi below is my Progarm , use while loop for that and a boolean to flag the loop
code :
boolean isValid = true;
int totalamount = 85000;
while(isValid){
  System.out.println("Please select type of Transaction 1.Balance Enquiry 2.Withdraw Money 3.Exit");
        int option = sc.nextInt();
        System.out.println("your choice is..." + option);
         if (option == 2) {
            System.out.println("enter amount to withdraw");
            int amount = sc.nextInt();
            System.out.println("Remaining Balance in your account is..."
                    + (totalamount - amount));
        } else if(option == 3){
           //exit
           isValid = false;
         }
        System.out.println("Do you want to continue? 1.yes 2.no");

        int lastPrompt = sc.nextInt();

        if(lastPrompt == 2){
         break; or isValid = false;
         }
}
Java - Using a scanner input to repeat a pattern

Java - Using a scanner input to repeat a pattern


By : user3763976
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm writing a program for a class assignment where a box shaped pattern must be made using for loops. The pattern is then repeated horizontally a certain number of times based off of a scanner input. For example the scanner input is 3 so the pattern is repeated three times horizontally.
code :
for (int i=0; i<Integer.valueOf(scannerInput); i++) { // where scannerInput is the String input that is obtained from user
    printPattern(); // System.out.println() call
}
Repeat File Printing Using Scanner

Repeat File Printing Using Scanner


By : Mohamed Ahmed
Date : March 29 2020, 07:55 AM
I hope this helps you . You can put the fileSearch into the while loop so that it finds the file each time you re-search through the loop. You just call UI(console); in the main then.
Also, I would recommend closing the scanner at the end, as it may not be used again (unless of course c is re-entered).
code :
public static void UI(Scanner console) throws FileNotFoundException {
        String choice = "Start";
        while (!choice.equals("q")) {
            Scanner fileSearch = new Scanner(new File("simple.txt"));
            System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
            choice = console.next();
            choice = choice.toLowerCase();
            if (choice.equals("c")) {
                System.out.println("Create");
                CreateMadLibs(fileSearch);
            } else if (!choice.equals("q")) {
                System.out.println("I don't understand.");
            }
            fileSearch.close();
        }
    }
Why does java.util.scanner repeat loop for every space

Why does java.util.scanner repeat loop for every space


By : Appy
Date : March 29 2020, 07:55 AM
it helps some times Its because next() does not consume the whole line. It only consumes the first word until space and the remaining words stays.
Thus when your loop will repeat as many words you have until it finds an int. For example here you are giving P P P means 3 words hence thrice iteration
Related Posts Related Posts :
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • Elegant way to create one of a large number of classes
  • Java fx binding styles
  • Summing weights based on string in large file
  • Android ListView setOnItemClickListener not working
  • How is the Java Bootstrap Classloader loaded?
  • Check object is blank
  • Add image between text in JEditorPane
  • Java POI Excel creating new column and new rows
  • NullPointerException when managing bean with Spring+JSF(Primefaces)
  • Content autocomplete in spring tool suite 4
  • Super Noob Help ~ MouseEvent/Frame
  • Making one project with dependencies stand-alone in Eclipse (Java)
  • Jsoup- getting certain attributes from website
  • MongoDB, how to make a link between collections with JAVA code
  • Overriding an abstract method means class abstract class or not in java?
  • Bukkit teleport - nullPointerException
  • How to check if a Float variable is negative in java
  • Servlet: Cannot forward after response has been committed
  • sql query to select a specific cell in excel
  • How to make new picture when mouse dragged in java
  • Use Constants interface in GWT Project
  • Setting size of a JPanel in GroupLayout
  • Construct a map from two equal size arrays
  • Implementing Read-Write Locks with Double-Checked Locking
  • Disable NO_BUTTON from JOptionPane Java if a condition is true
  • UDP multiple socket
  • Android Threading: This Handler class should be static or leaks might occur
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org