• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having difficulty with "Scanner console"

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have about two weeks of experience with JAVA and my question is thus. I wrote some code with help from the text and in each instance I pop it into JGrasp and it compiles ok. I try to run the code and it just keeps running and running and running.

Here it is. Is there something I am missing? Should i try another like Netbeans and see if I get a solution?



import java.util.*;

public class SecondJavaProgram
{

static Scanner console = new Scanner(System.in);

public static void main(String[] args)

{

int num1;
int num2;

num1 = 4;
num2 = console.nextInt(2);

System.out.println(num1+""+num2);


}
}


OR









import java.util.*;

public class SecondJavaProgram
{

static Scanner console = new Scanner(System.in);

public static void main(String[] args)

{

int num1;
int num2;

num1 = 4;
num2 = console.nextInt(0);

System.out.println(num1+""+num2);

// this has the zero in console.nextInt
//this gave me a string of results and also a "00"





 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nextInt(radix) -- you are in one case passing it a radix of 0, doesn't even seem valid to me. In the other case you are passing in a radix of 2 which would expect a binary number. If you just want to enter a normal base 10 number use nextInt().
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And about the code continuously running, just want to confirm if you are entering any integer in the console and pressing enter.
Else the code will sure keep running, waiting for the input
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is verbatim from the text , this will run and run and run in Jgrasp. The application compiles without issue.





import java.util.*;
public class Example2_16
{

static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{

int feet;
int inches;

System.out.println("Enter two integers seperated by spaces.");

feet = console.nextInt();
inches = console.nextInt();



System.out.println("feet = " +feet);
System.out.println(inches = " + inches);


}
}
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I entered 23 and 7 in each console (), It just gives

"Enter two integers seperated by spaces.
2"

and while giving this result it keeps running.
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see in the code that you are invoking the method nextInt() twice. It will be expecting two integers. The code will keep running if not provided with the inputs.
Have you entered the first number and the pressed enter, key in the second number and pressed enter again ?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are missing a double-quote in front of the word 'inches'. Other than that I got it to compile and run in Eclipse. I got

Enter two integers separated by spaces.
10 20
feet = 10
inches = 20

 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks Carey,

Any idea about why JGrasp is acting in this manner? It seems like such a basic thing for it to trip up. I have the 32 bit by the way.
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
feet = console.nextInt();
inches = console.nextInt();

I did it with

feet = 10
inches = 20

which worked.
So why use "console.nextInt" What is this code use?
reply
    Bookmark Topic Watch Topic
  • New Topic