7 Replies - 930 Views - Last Post: 11 August 2010 - 06:30 PM Rate Topic: -----

#1 roohan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-June 10

C++ to Java

Posted 10 August 2010 - 03:37 PM

everything seems so simple in java except the I/O stream. I have my program due in 2days and ive done everything but the I/O, people say it takes forever? so like whats the fastest and easiest way to input into a string or char? and whats the easiest way to output the same? like the basic and most simplest way ? Thanks...
Is This A Good Question/Topic? 0
  • +

Replies To: C++ to Java

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: C++ to Java

Posted 10 August 2010 - 03:41 PM

Scanner scan = new Scanner(System.in); for input
System.out.print(whatever}; is the equivalent of cout <<
Was This Post Helpful? 0
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: C++ to Java

Posted 10 August 2010 - 03:44 PM

How does this have anything to do with C++ to Java?

As for your question, the Scanner class is commonly used for input and to output in your console...you just use System.out.print()...
Scanner in = new Scanner(System.in);
String userInput = in.nextLine();
System.out.println("Your input was: " + userInput);


Note: you must import the Scanner class from java.util package
Was This Post Helpful? 0
  • +
  • -

#4 roohan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-June 10

Re: C++ to Java

Posted 10 August 2010 - 03:48 PM

oh coz iam from C++ and just switched to java.sry abt that... so i gotta do import java.util at the top? and thats it ? people talked about adding classes and classes to do this kinda stuff? whats that abt ?
PS: Garbage collection FTW


View Postbcranger, on 10 August 2010 - 02:44 PM, said:

How does this have anything to do with C++ to Java?

As for your question, the Scanner class is commonly used for input and to output in your console...you just use System.out.print()...
Scanner in = new Scanner(System.in);
String userInput = in.nextLine();
System.out.println("Your input was: " + userInput);


Note: you must import the Scanner class from java.util package

Was This Post Helpful? 0
  • +
  • -

#5 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: C++ to Java

Posted 10 August 2010 - 03:58 PM

// imports the entire util package...
// * means ALL
import java.util.*;
// imports just the Scanner class from the util package
import java.util.Scanner;


Was This Post Helpful? 0
  • +
  • -

#6 roohan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-June 10

Re: C++ to Java

Posted 10 August 2010 - 04:32 PM

does the scanner also output? scanner.out?

View Postbcranger, on 10 August 2010 - 02:58 PM, said:

// imports the entire util package...
// * means ALL
import java.util.*;
// imports just the Scanner class from the util package
import java.util.Scanner;


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: C++ to Java

Posted 10 August 2010 - 04:38 PM

No use System.out.print() or System.out.println(); witha LineFeed as
bcranger showed you

Scanner in = new Scanner(System.in);
String userInput = in.nextLine();
System.out.println("Your input was: " + userInput);


Was This Post Helpful? 0
  • +
  • -

#8 garytkm89  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 28-February 09

Re: C++ to Java

Posted 11 August 2010 - 06:30 PM

Hi, I believe you might want to read about the difference in System.out.println(), System.out.print(), etc. as each of them serve a different purpose and also something like:

Scanner sc = new Scanner(System.in);
int temp = sc.nextInt();
double temp2 = sc.nextDouble();



nextInt() and nextDouble is used for different purpose.
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1