What's wrong with this code?

Put your programming skills to the test in these challenges.

What's wrong with this code?

Post by ghostheadx2 on Tue Jun 02, 2015 7:26 pm
([msg=88300]see What's wrong with this code?[/msg])

Hi, so I was working on tomorrow's lecture in advance and I was wondering, in between making flashcards, if I could test out some of the methods they were going to teach. I made a program with a list of the methods, but it won't quite work. I'm wondering why I'm getting incompatible types and cannot find symbol errors. Here is my code:

Code: Select all
import java.util.Scanner;

class inputFunctions{
   public static void main(String args[]){
   System.out.println("Enter a value: ");
   Scanner input = new Scanner(System.in);
   double a = input.next();
   System.out.println(a);
   double b = nextByte();
   System.out.println(b);
   double c = input.nextShort();
   System.out.println(c);
   double d = nextLong();
   System.out.println(d);
   double e = input.nextFloat();
   System.out.println(e);
   double f = input.nextDouble();
   System.out.println(f);
   double g = input.nextBoolean();
   System.out.println(g);
   }
}


Obviously, this is either a syntax or a runtime error.

:D

-- Tue Jun 02, 2015 7:28 pm --

I'm an idiot. Some of these things needs to be strings. Lol.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: What's wrong with this code?

Post by -Sandman- on Tue Jun 02, 2015 9:55 pm
([msg=88301]see Re: What's wrong with this code?[/msg])

DISCLAIMER!
I am still newbie at Java and programming so the things I might say can be wrong or misleading. :D

I tried to compile your code and here is the result.
Whose nextByte() and nextDouble() method are you trying to call?
I think you can sort out rest of the error messages.
Have fun. :)
User avatar
-Sandman-
New User
New User
 
Posts: 25
Joined: Thu Jan 23, 2014 12:26 pm
Blog: View Blog (0)


Re: What's wrong with this code?

Post by sanddbox on Tue Jun 02, 2015 11:22 pm
([msg=88303]see Re: What's wrong with this code?[/msg])

According to your code, booleans, longs, and shorts are all doubles. You need to choose the correct variable type.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2344
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: What's wrong with this code?

Post by ghostheadx2 on Wed Jun 03, 2015 3:57 pm
([msg=88313]see Re: What's wrong with this code?[/msg])

Thats what I thought. So then, I made my code this:

Code: Select all
import java.util.Scanner;

class inputFunctions{
   public static void main(String args[]){
   System.out.println("Enter a value: ");
   Scanner input = new Scanner(System.in);
   String a = input.next();
   System.out.println(a);
   byte b = input.nextByte();
   System.out.println(b);
   short c = input.nextShort();
   System.out.println(c);
   long d = input.nextLong();
   System.out.println(d);
   float e = input.nextFloat();
   System.out.println(e);
   double f = input.nextDouble();
   System.out.println(f);
   boolean g = input.nextBoolean();
   System.out.println(g);
   }
}


So then I get an error with these two lines:

Code: Select all
   String a = input.next();
   System.out.println(a);


I get this error:

Code: Select all
Exception in thread "main" java.util.InputMismatchException
   at java.util.Scanner.throwFor(Scanner.java:909)
   at java.util.Scanner.next(Scanner.java:1530)
   at java.util.Scanner.nextShort(Scanner.java:2030)
   at java.util.Scanner.nextShort(Scanner.java:1989)
   at inputFunctions.main(inputFunctions.java:11)


What do you make of that?
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: What's wrong with this code?

Post by sanddbox on Wed Jun 03, 2015 11:25 pm
([msg=88323]see Re: What's wrong with this code?[/msg])

What are you inputting for the values? Are you entering values of the right type?
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2344
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: What's wrong with this code?

Post by ghostheadx2 on Thu Jun 04, 2015 12:41 pm
([msg=88329]see Re: What's wrong with this code?[/msg])

It won't give me the error for anything else, with the improved code at least, just with the Strings. I try to put multiple word lines into it and it won't let me. That's what I want to do. It will let me put whatever 1 word value I want otherwise.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: What's wrong with this code?

Post by sanddbox on Thu Jun 04, 2015 5:16 pm
([msg=88333]see Re: What's wrong with this code?[/msg])

It's been a while since I've used the scanner class, but what's the default delimiter? I'd imagine it's space and that's why it's not working for you. If it's splitting on the space then you're entering a String[], not a String. In that case, changing the delimiter to newline would do the trick.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2344
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests