How to read input with multiple lines in Java

后端 未结 10 1073
名媛妹妹
名媛妹妹 2020-12-05 02:48

Our professor is making us do some basic programming with Java, he gave a website and everything to register and submit our questions, for today I need to do this one exampl

相关标签:
10条回答
  • 2020-12-05 03:49
    package pac001;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class Entry_box{
    
    
    
        public static final String[] relationship = {"Marrid", "Unmarried"};
    
    
        public static void main(String[] args)
        {
             //TAKING USER ID NUMBER
                int a = Integer.parseInt(JOptionPane.showInputDialog("Enter ID no: "));
            // TAKING INPUT FOR RELATIONSHIP
            JFrame frame = new JFrame("Input Dialog Example #3");
            String Relationship =   (String) JOptionPane.showInputDialog(frame,"Select Your Relationship","Married",
                                    JOptionPane.QUESTION_MESSAGE, null, relationship,relationship[0]);
    
    
    
            //PRINTING THE ID NUMBER
            System.out.println("ID no: "+a);
            // PRINTING RESULT FOR RELATIONSHIP INPUT
              System.out.printf("Mariitual Status: %s\n", Relationship);
    
            }
    }
    
    0 讨论(0)
  • 2020-12-05 03:50
    import java.util.*;
    import java.io.*;
    
    public class Main {
        public static void main(String arg[])throws IOException{
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            StringTokenizer st;
            String entrada = "";
    
            long x=0, y=0;
    
            while((entrada = br.readLine())!=null){
                st = new StringTokenizer(entrada," ");
    
                while(st.hasMoreTokens()){
                    x = Long.parseLong(st.nextToken());
                    y = Long.parseLong(st.nextToken());
                }
    
                System.out.println(x>y ?(x-y)+"":(y-x)+"");
            }
        }
    }
    

    This solution is a bit more efficient than the one above because it takes up the 2.128 and this takes 1.308 seconds to solve the problem.

    0 讨论(0)
  • 2020-12-05 03:52

    The problem you're having running from the command line is that you don't put ".class" after your class file.

    java Practice 10 12

    should work - as long as you're somewhere java can find the .class file.

    Classpath issues are a whole 'nother story. If java still complains that it can't find your class, go to the same directory as your .class file (and it doesn't appear you're using packages...) and try -

    java -cp . Practice 10 12

    0 讨论(0)
  • 2020-12-05 03:55

    Use BufferedReader, you can make it read from standard input like this:

    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    String line;
    
    while ((line = stdin.readLine()) != null && line.length()!= 0) {
        String[] input = line.split(" ");
        if (input.length == 2) {
            System.out.println(calculateAnswer(input[0], input[1]));
        }
    }
    
    0 讨论(0)
提交回复
热议问题