Please tell me how to get a long Java string (including spacing) and get an int and a string againㅠ n Error in nextLine()

Asked 2 years ago, Updated 2 years ago, 104 views

//Using the Scanner class // Name, age, height, weight (up to decimal point 2), city, address (must be received even after spacing), and single status //print it out


import java.util.Scanner; 
public class J1 { 

  public static void main(String[] args) { 

    Scanner sc = new Scanner(System.in); 
    System.out.println ("Name, Age, Weight, City Address, Single: "); 
    String name = sc.next(); 
    System.out.print("Name" +name); 
    int age = sc.nextInt(); 
    System.out.print ("age is "+age"); 
    int height = sc.nextInt(); 
    System.out.print ("Key is "+height"); 
    double weight =sc.nextDouble(); 
    System.out.print ("Weight is "+weight"); 
    String city = sc.next(); 
    System.out.print ("City is "+city"); 
    sc.nextLine(); 
    String address=sc.nextLine(); 
    System.out.print ("address is "+address"); 
    Boolean isSingle = sc.nextBoolean(); //true 
    System.out.print ("You are single or not "+isSingle+"); 
    sc.close(); 
   }
}

Originally, next() and nextInt() are spaced apart,

I know that the nextLine() stops receiving space in the enter so that it can receive space,

Write down the address like above and press enter.

The previous ones (name to address) are printed out...

What's wrong with this?

How do I get it to the end (even single) and print it out at once?

Instead of making a variable and printing it once at the end...

java scanner nextline

2022-09-22 19:15

1 Answers

Are you curious about the reason why this is happening?

Calling a method of the next class of Scanner receives user input.

Count how many methods start with the next in the code written by the questioner. You're going to get that number of inputs.

And the reason why they're printed is also simple.

This is because the code written by the questioner is printing the results that you entered each time on the screen.

"Not how to make a variable and print it out at once at the end." <- Didn't you write the code above to do this?

Do you want to get the results after receiving the input of name, age, weight, city address, and single status by comma? (You have to be good at asking questions. Don't forget that good answers come from good questions.)

If what I expected is right...You can get one line with the nextLine and get it as a token by dividing it with commas through the split method of the String class.

import java.util.*;
import java.util.stream.*;
...
Scanner sc = new Scanner(System.in); 
System.out.println ("name, age, height, weight, city, address, single status: ");
String line = sc.nextLine();
String[] datas = line.split(",");
Stream<String> dataStream = Arrays.stream(datas);
dataStream.forEach(i -> System.out.println(i));
...

Input: aaaa, 10,100, 50.0, Seoul, Korea Seoul, true
Output:
aaaa
10
100
50.0
seoul
korea seoul
true


2022-09-22 19:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.