How to make this program return an entire sentence.

Put your programming skills to the test in these challenges.

How to make this program return an entire sentence.

Post by ghostheadx2 on Wed Jun 03, 2015 4:03 pm
([msg=88314]see How to make this program return an entire sentence.[/msg])

So I fixed my Java program to look better after I made a stupid mistake. I asked a dumb question here on HTS, but now I have a legitimate question. I wrote this code and made it work for the most part:

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);
   }
}


When I try to use a value that's a whole sentence, I find that this doesn't work. If it asks me for a value and I type:

Code: Select all
Enter a value:
hi I'm bob


It returns this:

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)


It won't do that if I use a single word or any number. It will instead return that word, number, etc. I'm looking for a way to return an entire sentence. Could somebody help me out here?
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: How to make this program return an entire sentence.

Post by ghost107 on Wed Jun 03, 2015 6:55 pm
([msg=88316]see Re: How to make this program return an entire sentence.[/msg])

According to the documentation:
http://docs.oracle.com/javase/8/docs/ap ... anner.html

next wrote:public String next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

Specified by:
next in interface Iterator<String>
Returns:
the next token
Throws:
NoSuchElementException - if no more tokens are available
IllegalStateException - if this scanner is closed
See Also:
Iterator

Since you didn't specify a delimiter usually the default delimiter is any white space character, so when you wrote "hi I'm bob", so a = "hi", b = "I", and then c will expect to read a short(number with value from -32,768 to 32,767), then it tries to read the character " ' " which is not a number.

If you want to read a string, try using nextLine:\
Code: Select all
Scanner input = new Scanner(System.in);
String a = input.nextLine();

nestLine wrote:public String nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Returns:
the line that was skipped
Throws:
NoSuchElementException - if no line was found
IllegalStateException - if this scanner is closed

Or use a delimiter, to your next.
Code: Select all
Scanner input = new Scanner(System.in);
input.useDelimiter("\\n");
String a = input.next();
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: How to make this program return an entire sentence.

Post by ghostheadx2 on Wed Jun 03, 2015 8:26 pm
([msg=88318]see Re: How to make this program return an entire sentence.[/msg])

My only problem with this is that some numbers will return this error and some won't. I can tell with double and float values:

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.nextByte(Scanner.java:1924)
   at java.util.Scanner.nextByte(Scanner.java:1883)
   at inputFunctions.main(inputFunctions.java:10)


With 4.3 as the value it gives me that error. But when I type 5.5, there's no difference. That's when I use the delimiter like 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);
   input.useDelimiter("\n");
   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);
   }
}
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: How to make this program return an entire sentence.

Post by ghost107 on Thu Jun 04, 2015 7:51 pm
([msg=88338]see Re: How to make this program return an entire sentence.[/msg])

Try to use nextLine() for reading lines of text(read everything till the Enter key is pressed), or set the delimiter to "\\n" not "\n".
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests