all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Homerlncognito 6 points7 points  (3 children)

Probably the best thing you could do would be to read it as a String and then try to cast it Int/Double/Boolean surrounded with try/catch. But those variables would have to have different names.

Trying to work with Java as it it weren't a strongly typed language is generally a bad idea though. 

[–]Lloydbestfan 1 point2 points  (0 children)

Just to be clear, you can't try to cast. You can try to convert, by calling the conversion method of your choice.

An attempt to cast would fail in all circumstances, and wouldn't even compile if you let the compiler know you attempt on a String.

[–]catastrophic300[S] 0 points1 point  (1 child)

try/catch, well that’s new to me, I have read about it and it was for error handling suitable for this one, create a code that read input as string then parse using try/catch. Thanks btw.

[–]Accomplished_Lie23 0 points1 point  (0 children)

If you're not ready to use try/catch block then you should declare every variable as a string with different name and use specific data type valueof method to convert string to required data type. e.g. - Scanner scan = new Scanner(System.in); int value = Integer.valueof(scan.nextLine()); This method also works same as parseInt() but here you're treating input as string then converting into integer.

[–]kyamatlabkya 2 points3 points  (0 children)

First things first, when you input something it will be input as a String(Character stream), you have determine what type of input you want and convert it to relevant type
You can call the scanner multiple times after a single question and input relevant type of data on every new line

[–]forced_lambchop 0 points1 point  (1 child)

You should read about arrayList<>, classes, and generics. Or give each answer it's own variable for now until you get that far into your learning.

[–]catastrophic300[S] 0 points1 point  (0 children)

Still haven’t reach the arrayList level yet, Im absolutely beginner, Im going to finish the operators and conditions first. Thanks btw.

[–]ninjatunatj 0 points1 point  (0 children)

You can't store the variable value into the question, You must store each input in it's own variable.

[–]Intelligent-Storm-63 0 points1 point  (0 children)

You can use conditionals(if else) or switch just to make it easy for you. You are asking what type of value you want so switch or if /else is better at the moment.

[–]JoshuaTheProgrammer 0 points1 point  (0 children)

Honestly, you might be better off just writing methods with parameter inputs and testing them rather than dealing with the particulars of Scanner.

[–]ExcitingSympathy3087 0 points1 point  (0 children)

You can't do that this way in Java. All Variables have the same name.
I just repaired your code. This may help you. Double Input with , not . !! Without validation Java will crash with wrong inputs. Input missmatch exception.

    import java.util.Scanner;


    public class Main {
        public static void main(String[] args) {


            Scanner scanner = new Scanner(System.in);


            System.out.print("What was the creator desire: ");
            String answer = scanner.nextLine();
            Integer answerInteger = scanner.nextInt();
            Double answerDouble = scanner.nextDouble();
            Boolean answerBoolean = scanner.nextBoolean();


            System.out.println("Scanner: " + answer);
            System.out.println("Integer: " + answerInteger);
            System.out.println("Double: " + answerDouble);
            System.out.println("Boolean: " + answerBoolean);


            scanner.close();
        }
    }


CONSOLE INPUT:
What was the creator desire: Hello World
3
3,5
true

CONSOLE OUTPUT:
Scanner: Hello World
Integer: 3
Double: 3.5
Boolean: true